How to add force in Position X direct of travel
Hello
I know I can add this line to add force in the direction of travel to an object with a rigidbody:
rigidbody.AddForce(rigidbody.velocity.normalized * Time.deltaTime * forceAmount);
However, I need help in finding a way to add force for only X or -X when the object is travelling in either X direction. I.e. if the object is travelling along -X, force should be applied to -X and vice versa for X.
Does anyone know how to manage that? Many thanks!
Answer by tormentoarmagedoom · May 20, 2019 at 09:40 AM
Hello.
I'm not sure what are you asking, because its like you know the answer but dont see it...
if want add force in X axis, just:
rb.Addforce(new Vector3(1,0,0);
Now, you can multiply/divide as you want, but this force will have only X component...
Whats the question ?
Answer by Reject76 · May 21, 2019 at 05:00 PM
Yes, close, but this is what I'm after:
If the object is travelling in -X direction I want to Addforce -5 on the X axis. If the object is travelling in X direction, I want to Addforce 5 on the X axis.
I'm building a pong-style game and the main problem I have is with the ball getting stuck endlessly bouncing back and forth on the Y-axix betweet my upper and lower border walls (player paddles are left and right).
Hence I need something that shoves the ball either left or right (i.e. X or -X) when the ball hits something on the playfield (wall, paddle or other object).