Unity - How to Jump

Matthew·2024년 8월 22일
0

Unity

목록 보기
9/9
post-thumbnail

How to Jump

Just "jumping" is easier than I thought.

  • Make a variable for our jumpingPower, and use it to control our jump heights.
if (Input.GetButtonDown("Jump"))
        {
            rigid.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
        }
  • We're adding force to our rigidbody 2D, so wirte rigid.Addforce.
  • We're jumping, which means up. Therefore, do Vector2.up and multiply by the number of jumpPower.

  • Now we are able to make our gameObject jump. However, you can jump infinitely.
  • This can be solved by checking if the player is touching the ground or not.

How to Check Ground with Boxcast

Pseudo Code:
If the player is touching the ground and pressing jump button, the player can jump.
If the player is not touching the ground, the player can't jump.

  • Based on the pseudo code, we first need to know if our player is on the ground or not.
  • We will use Physics2D.Boxcast() to check if the player is on the ground or not.

Physics.Boxcast() creates a box-shaped volume and detects collision, which returns a boolean value.

Boxcast Parameters:

Physics2D.Boxcast(Vector2 origin, Vector2 size, float angle, Vector2 direction, float distance, int layerMask)

Make Variables for the Ground Check

We need to make variables that will go into the Boxcast parameters.

  • Vector2 to size our cast box.
  • float value to control the distance between the player's position and box cast.
  • Layermask to check which layer we are detecting for.

public bool IsGround()
    {
        if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, distance, groundLayer))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
  • We will make a new bool called IsGround() so we can use it to check if the player is on the ground.

  • We can plug our variables into the Boxcast parameters.
    I don't want any angles so I put 0 as a angle value.
    I want to check what's under our player, so I did -transform.up.

    There's no transform.left or transform.down in transform components.

  • Since it's a boolean, we'll return true if our Boxcast is detecting the ground, and return false if it's not detecting any ground.

Visualize the Boxcast

Boxcast is not visible by default, so it's difficult to control it.

private void OnDrawGizmos()
    {
        Gizmos.DrawWireCube(transform.position - transform.up * distance, boxsize);
    }
  • We'll use void OnDrawGizmos().

    void OnDrawGizmos() is a tool that draws the visual represent for the gizmos. It's primarily used to debug or visual purposes.

  • We can use Gizmos.DrawWireCube to visualize our Boxcast.

    Gizmos.DrawWireCube draws wired cube to visualize the box.

  • Use (transform.position - transform.up * distance, boxsize);

    We want our center for the box to be same as Boxcast, which is why we did transform.position - transform.up. Then, we multiply it by the distance value to control the boxcast's y-axis location.

  • Now we can visualize Boxcast and control it.

Make Ground Layer in the Layer component

Now we can make a Layer called "Ground" to detect the ground.

  • Click Layer that's on the inspector window, and click Add Layer.

  • Add the layer and name it "Ground".
  • Make sure you select the layer after saving it.

  • Then, go to the player's script and select Ground layer.

Jump only Once using Ground Check

  • Now, we'll make a void Jump() and call it in Update() to jump.
 void Jump()
    {
        if (Input.GetButtonDown("Jump") && IsGround())
        {
            rigid.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
        }
       
    }
  • If the player is pressing jump button and the player is on the ground, we'll add force to the player to go up.

  • Now we can only jump when we are on the ground.
  • Make sure you called the void Jump() in the Update() and selected the groundLayer in the player's script.
profile
Growing Game Dev

0개의 댓글