UNITY2.5D Sprite level display occlusion problem processing

Source: Internet
Author: User

Code from the game "A Place for the unwilling"

The first problem to be solved in developing the A place for the unwilling game is to let the genie move around the other sprites, presenting a true sense of depth. The Spriterenderer component has two properties that can change the rendering order of sprites in the scene.

    • Sorting layer to set the sprite rendering order for different layers
    • The order in layer is used to set the sequence of sprite rendering in the same layer



If you want to change the rendering order of multiple sprites in real time, you need to modify some properties so that they are rendered in the correct order no matter how the sprite moves in the scene. Because the Oder in Layer property accepts only integer parameters, using the z-axis appears to be a better choice.

The rendering priority for sprites in unity is high-to-Low:

<ignore_js_op>



If the "sorting layer" and "Order in layer" of the two sprites are the same, the sprite closer to the camera in 3D world coordinates will be rendered first.

After understanding the rendering order of the Sprite, the next step is to write a simple script to change the z-value of the sprite coordinates to a fixed ratio of its Y value. But before you do, let's explain an important little concept, that is, how to set the sprite at the bottom of the ground. The "bottom" here refers to the part of the 3D world in which objects touch the ground, as shown in the following example:

<ignore_js_op>



What we're going to do is change the Y value of the sprite coordinate while changing its z-value, in the 3D environment, such as:

<ignore_js_op>



Understand the above, you can write the script, the code is as follows:

1 usingUnityengine;2 3 [Executeineditmode]4  Public classIsometricstaticobject:monobehaviour {5 6 [Serializefield]7     Private floatM_floorheight;8     Private floatM_spritelowerbound;9     Private floatM_spritehalfwidth;Ten     Private ReadOnly floatM_tan30 = Mathf.tan (Mathf.pi/5); One  A     voidStart () -     { -Spriterenderer Spriterenderer = getcomponent<spriterenderer>(); theM_spritelowerbound = Spriterenderer.bounds.size.y *0.5f; -M_spritehalfwidth = spriterenderer.bounds.size.x *0.5f; -     } -  +     //Use this condition to objects that don ' t move in the scene. - #ifUnity_editor +     voidlateupdate () A     { at         //Use this condition to objects that don ' t move in the scene. -         if(!application.isplaying) -         { -             //Update the position in the Z axis: -Transform.position =NewVector3 -                  ( in transform.position.x, - TRANSFORM.POSITION.Y, to(Transform.position.y-m_spritelowerbound + m_floorheight) *M_tan30 +                  ); -         } the     } * #endif $ Panax Notoginseng     voidOndrawgizmos () -     { theVector3 Floorheightpos =NewVector3 +                 ( A transform.position.x, theTransform.position.y-m_spritelowerbound +M_floorheight, + transform.position.z -                 ); $  $Gizmos.color =Color.magenta; -Gizmos.drawline (Floorheightpos + vector3.left * m_spritehalfwidth, Floorheightpos + vector3.right *m_spritehalfwidth); -     } the}

The first thing you need to set is "floor Height", which determines the offset of the bottom edge of the sprite in the y direction. In 3D world coordinates, it is used to set the z depth of the sprite in the scene. If the bottom of a sprite is higher than the other sprite, it will be rendered behind other sprites.

<ignore_js_op>



It then stores a half value of the height and width of the sprite in order to perform some simple mathematical operations on the z-coordinate. The 30-degree isometric chamfer is used in the A place for the unwilling game, but you can also set the z coordinate to match the y-coordinate without affecting the game effect.

Here, the Ondrawgizmos method is used to draw a line at the current ground height so that it can be set to the final precise position in the editor. In addition, for some games that never move after running the object, you can use the IF (! application.isplaying) "and" # if Unity_editor "conditions save the results at run time because there may be hundreds of sprites that bind the script at the same time.

When the above settings are complete, you can move the sprite in the scene and ensure that the rendering order is normal, but there are two more settings that need to be set.

When the sprite is not in the middle of the processing center, it needs to be divided into several parts. Take the following building, for example, because its bottom is a rectangle, and if the entire building only has a floor height value, the character will only walk along the front of it and will block the character! To solve this problem, it is necessary to divide the building sprite into two parts and set different ground for each side, such as:

<ignore_js_op>



Another scenario is when you use a sprite as another child object. In the case of buildings, for example, if you want to add Windows or signboards to a building, these additions will not be able to use the same script as the building, because some windows may be located behind or at the top of the building. This problem is easy to solve, simply create a building's sub-object to reset its coordinates, and set the z-coordinate value to-0.001, and then place all the objects that need to be attached to the building under the sub-object, set the z-coordinate of these objects to 0, so that they can maintain a 0.001 distance from the actual building, and they are closer to

<ignore_js_op>



The complete scenario in the final 3D environment is as follows:

<ignore_js_op>



The unity engine itself has provided a very flexible tool to implement such functionality, and here are some of the limitations of this implementation, as well as some extension methods to help improve the workflow.

The biggest limitation of this implementation is when making very thin walls, because using this method you must cut the sprite into several parts that are consistent with the thickness of the wall so that the objects in the scene can move around the wall. Examples are as follows:

<ignore_js_op>



It may also be troublesome for flying objects, but if you pay attention to the position you can avoid problems. You can also modify the values of the sorting layer so that they are always in front or behind the main object of the scene.

Finally, share how to extend this way to apply more scenarios.

Isometric colliders: Depending on how the character moves in the game, a small script is created to create a collision with the image of the game scene in the same way as the character.

<ignore_js_op>



Isovector class: This class contains some commonly used directional vectors (N,W,E,S,NE,NW,SE,SW), as well as methods to get vectors from a custom direction (or vice versa), or to get a reverse vector in a given direction (for example, input south to get North).

What this article describes is not necessarily the best solution, but it also shows a good way to learn, from the very beginning of the thought of writing a script to adjust the z-value of the sprite to correctly render all objects, solve the problem of the first build the game scene. As you continue to extend the code base, you have enriched some custom classes to incorporate new features while maintaining the project structure. Hopefully this article will help developers who are using unity to develop this isometric game!

Original link: https://madewith.unity.com/en/stories/what-i-learned-from-trying-to-make-an-isometric-game-in-unity
Original Martín Pane

UNITY2.5D Sprite level display occlusion problem processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.