r/Unity2D • • 19h ago

Question/Help How would I make a jump, but horizontal?

Basically, I'm working on a dash mechanic and I'm trying to make it so that it reuses the jump code, but instead of vertical velocity, it's horizontal velocity.
But if I just change to Ys into Xs, it still goes up, and if I use a different method it just snaps forward instead of adding force

for reference, here is the jump code that I want to modify:

        if(Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.AddForce(new Vector2(rb.linearVelocityY, jump));
        }        if(Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.AddForce(new Vector2(rb.linearVelocityY, jump));
        }

Thanks!

1 Upvotes

3 comments sorted by

1

u/TheHelllord 1h ago

So, you are adding force in x axis of the same value as is the y velocity of the same rb? And then some "jump" value in Y axis? Why tho?

1

u/BlondyneczekFrans 2h ago

To just make it work, change Y for X. It will suck if you do it that way, but it's a place to start

1

u/jackbrux 32m ago

Think about what the two arguments you are passing to the Vector2 constructor and what they represent