Unity advanced improvement – 2D game jumping feel optimization (jumping and falling)
When developing 2D games, jumping is an indispensable and important function. However, when we were developing in Unity, Unity’s own physics engine did not provide a very good feel. It felt light when falling, which was obviously very uncomfortable to operate. Therefore, we need to optimize the jump ourselves to get a better feel. It is not difficult to find that in most 2D games when jumping, the falling speed is much faster than the rising speed, and the falling time is about half of the rising speed.
Tip, when optimizing jumps, rigid body components will be used
First, we need to analyze the jumping phase of Unity objects:
In the picture, jumping is divided into two stages. The first stage is the jumping stage, the rising stage of the character, and the second stage is the falling stage
In the first stage, the upward velocity of the rigid body is positive, that is, velocity.y>0; in the second stage of falling, the object’s velocity.y<0, so we can determine the location of the object by the positive and negative velocity.y At which stage, objects can be manipulated more precisely.
Next, we have to operate the object’s jumping phase. First, we need to determine whether the object is on the surface, and press W or other jump buttons. After triggering the jump, we assign a velocity.y to the object. The initial value V represents the initial speed, then the change in the final jump height is h=Vt-1/2gt2 (g is the acceleration of gravity, t is the time), the object velocity velocity.y=V-gt, when velocity. When y=0, the object reaches the highest point. At this time, the height of the object (the highest point) H=(V2)/(2g).
Next, the object is still affected by the acceleration of gravity, and the value of velocity.y of the object continues to decrease and becomes a negative value, which also indicates that the object begins to fall. It can be seen from the figure that when an object only has an upward initial velocity and is only affected by gravity, its rise time and fall time are the same, T=V/g. Therefore, in order to ensure that the falling time is half of the rising time, we need to change the gravitational acceleration of the object to double its original value, that is, modify the value of Physics2D.gravity to multiply its y value by 2.
In this way we have completed a jump optimization. The code is as follows:
//Hint! This code uses rays to detect whether the object is on the ground
//hint! The meaning of each variable
//rb rigid body component
//jumpSpeed jump initial speed
//fullSpeed falling gravity acceleration multiple
//isGround Is the player on the ground?
//rayDistance ray distance
//groundLayerMask ground layer
void Update()
{
Ground();
if (Input.GetKeyDown(KeyCode.W) && isGround)
{
Jump();
}
if (rb.velocity.y <= 0)
{
Full();
}
}
private void Jump()
{
rb.velocity = Vector2.up * jumpSpeed;
}
private void Full()
{
//Gravity acceleration becomes fullSpeed times
Physics2D.gravity = new Vector2(0, -9.8f * fullSpeed);
}
//Ray detection, whether it is on the ground
private void Ground()
{
//Cast a ray from underneath the object to see if it collides with the ground
RaycastHit2D hit=
Physics2D.Raycast(transform.position,Vector2.down,rayDistance,groundLayerMask);
if (hit.collider != null)
{
isGround = true;
}
else
{
isGround = false;
}
}