A
A
Artem Zuev2020-11-18 22:45:23
Unity
Artem Zuev, 2020-11-18 22:45:23

How to make a character touch the ground in Unity 2D?

I'm making an android game using Unity 2D. I have character jump code:

public void Jump(bool isJump)
    {
        isJump = groundCheck;

        if (groundCheck)
        {
            rb2d.velocity = new Vector2(rb2d.velocity.x, jumpPower);
        }

    }

I tried to check if the character is on the ground in this way:
void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "ground")
        {
           groundCheck = true;
       }
    }

But that jump doesn't work at all. How do I make it so that the character can only jump if he is standing on the ground.
I'm new to C# programming. Please help me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Ro, 2020-11-19
@VictorRo

It does not work, most likely, because initially the variable groundCheckis equal falsein the declaration, you need to set it immediately true, because initially the character is already on the ground: And after the jump, immediately change to :
bool groundCheck = true;

if (groundCheck)
        {
            rb2d.velocity = new Vector2(rb2d.velocity.x, jumpPower);
            groundCheck = false;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question