- Home /
How to set velocity to a previouse value
Hi all
I am making a small pong style game and have a problem when the ball hit the paddle at the bottom it will some times slow down. Guessing it's when I move it as the ball is hitting it.
I have stored the last correct velocity in a vector3 and want to set it if the ball speed drop below that value.
If I just set the velocity of the rigidbody on my ball to my saved velocity, it also changes the direction of the ball to what it was when I stored it.
How do I keep my ball direction, and only set the velocity of the ball to my stored vector3?
Thx Rasmus
Answer by rutter · Jul 07, 2012 at 10:53 PM
How do I keep my ball direction, and only set the velocity of the ball to my stored vector3?
Velocity represents both speed and direction, which means you are implicitly assigning a direction when you assign velocity.
If I had to guess, you actually want to keep the old speed, but move in the current direction? If so, some quick vector multiplication can achieve that:
//produce a new velocity vector:
// - use same magnitude (speed) as current velocity
// - use previous direction
rigidbody.velocity = rigidbody.velocity.magnitude * lastVelocity.normalized;
I have already tried this and the problem is that it makes the ball change direction. I only want to add force to the ball, not change direction.
Example : Ball hits the paddle and slows down, it's going in the right direction, but to slow.
I detect this be looking at it's magnitude. I then want to keep the direction the ball is going in now, but set something on the rigidbody that will increase it's magnitude back to the original value.
$$anonymous$$y game works fine 95% of the time, but not if I move the paddle really fast to catch a ball, then it happens.
Perhaps you should try rigidbody.velocity = rigidbody.velocity.normalized * $$anonymous$$imumSpeed
That was exactly what I needed, thx.
Do I just accept the entire answer or can I only except whydoidoit's answer as it's the correct one?
Just accept the whole answer. I was just popping in to help :)
Your answer
Follow this Question
Related Questions
Comparing rigidbody speeds 1 Answer
Rigidbodies with fixed joint connection rotate instead of forward/back 1 Answer
Error on 'velocity = [Vector3].normalized'? 1 Answer
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
2d game - looking toward velocity for rigidbody player 1 Answer