D
D
d1zz72020-05-18 15:03:17
C++ / C#
d1zz7, 2020-05-18 15:03:17

Unity 2D diagonal jump?

Unity is a kettle, don't judge too harshly. There is a jump code:

void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            BallJump();
        }
    }

    void BallJump()
    {
        rb.AddForce(transform.up * 14f, ForceMode2D.Impulse);
    }

The code works, the object jumps. But, the object jumps up, but I would like it to jump diagonally, that is, up and forward at the same time, as in the game Dunk Hit .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2020-05-18
@d1zz7

Because you are applying force upwards transform.up. Take the direction vector you need.

D
d1zz7, 2020-05-18
@d1zz7

Problem solved, thanks everyone!

void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            BallJump();
        }
    }

    void BallJump()
    {
        Vector2 jjump = new Vector2(rb.velocity.x + 15f,rb.velocity.y + 10f);
         rb.AddForce(jjump, ForceMode2D.Impulse);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question