My mouvment script is locking my x velocity to 0 if i am not moving

hi
to move in my game i use this code

direction = Input.GetAxis("Horizontal"); 
 rb.velocity = new Vector2 (speed*direction, rb.velocity.y);

and it works but now when i want to launch the player in a direction using a botton the y axis work but x axis doesnt, i know because the mouvment script is making the x velocity 0 every frame but i dont know how to fix it

Two possible ways, depending on your game design:

  1. inhibit (block) the above code while the player is flying through the air (or for perhaps a short time)
  2. OR add the current x velocity (rb.velocity.x) with the inputs before setting the velocity.

is there a way to add velocity not set the velocity in any way??
i used addforce and movepos but it made the player fall slowly

That would be the option #1 posted above.

do you mean the 2nd if so how can i do it
i am very new to unity

Please don’t make duplicate posts.

This is the original post here: I want to launch my player in a direction but it only launches in the y axis - #6 by MelvMay

It sounds like perhaps you’re in over your head here and that’s not gonna be easy for anyone to solve here. The solution will NOT only be code: it will be code AND it will be other setup stuff.

I recommend doing tutorials for a game that has similar mechanics to what you are contemplating.

NOTE: there are only two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.