Simulate 3D environment and terrain from Unity 2D perspective

If 2D games want to imitate the environment of 3D games, a good solution is "2.5D".

The so-called 2.5D is generally understood to be the game perspective where the camera is fixed and looking down. From this perspective, characters can move on the x, y, and z axes, which can simulate the environment of a 3D game, and the art cost is much lower than that of 3D.

There are many ways to implement 2.5D, such as 2D character + 3D scene, 3D character + 2D scene map, 2D character + 2D scene map. This article discusses the last type. Since the characters and scenes all use 2D pictures, the essence can still be classified as a 2D game.

Demo demo:

Vector3 v3=new Vector3();
float pi=3.14159265f;
float pi_180=pi/180;
if(performer!=null)
{
    v3[0] = ((float)x);//和本体相同
    v3[1] = ((float)(Mathf.Cos(30 * pi_180)*y+Mathf.Sin(30 * pi_180)*z));//将三维坐标映射给二维贴图,其中30是根据2D图片假设的相机角度,pi_180是将角度转换成弧度进行计算。
    v3[2] = ((float)z*0.01f);//用于控制遮挡顺序,和本体成正比即可
    performer.transform.position=v3;
}

A simple method is used here: separate the character ontology (logic) and the character map (expression), and use trigonometric functions to calculate the position of the character map in the 2D scene.

Assuming that the camera angle is α, the character map position is (x2, y2), and the character logical position is (x, y, z), it can be seen that:

x2=x

y2=cos(α)*y+sin(α)*z

In this way, a three-dimensional world is simulated from a 2D perspective. On this basis, Unity's own physical system can also be used to design a 3D terrain to realize functions such as slopes and arch bridges. The renderings are as follows:

 Since the character body is still in the 3D space, you can directly add the collision body in the 3D space to realize the terrain (you can see that the collision body and the texture do not fit together, and the reason will be explained later)

So how to create a collision body based on a 2D texture? The formula for mapping from three dimensions to two dimensions was mentioned before, and conversely, mapping from two dimensions to three dimensions is also possible. However, there are infinite possibilities for mapping a point in two dimensions to three dimensions. It is not feasible to directly apply the formula. Therefore, we can only assume that y or z is a fixed value and calculate the relative coordinates through the change of the other value.

Suppose the camera angle is α, the map point is (x2, y2), the logical point is (x, y, z), and one grid is one unit.

As shown in the figure, Δz=0, Δy2≈1.92 

Using the formula, we get Δy2=cosα*Δy+sinα*0, and y=y2/cosα. At this time, I set α to 30°, so Δy≈2.217

 In the same way, we get Δz≈5.688

 Just apply it to the collision body

 It can be found that the mapped y is already larger than the two-dimensional y, so the collision body and the texture do not fit well in the two-dimensional perspective.

↑Width test, in line with expected results

The angle of the ramp must be processed in the same way. Three-dimensional slope angle = two-dimensional slope angle/cosα. The camera angle here is 30°, the angle of the ramp map is 15°, and the slope angle of the collision body is 17.32°. .

For arch bridges, the ellipse formula is applied, x^2/a^2+ (y/cosα)^2/b^2 = 1

After setting up the scene, you still need to set up the logic for the character to go uphill and cast shadows. For details, please see this article:

Unity’s research on character uphill_Dugu Xingye_’s blog-CSDN blog

Guess you like

Origin blog.csdn.net/qq_59141650/article/details/131335713