- Home /
How could I make a bouncy object move mostly along the Z axis?
So I have this ball, that I want to move along the long hall, at the end of which is a target.
So far I've managed to make the ball bounce, but it always ends up bouncing along the X and Y axes. How could I make it bounce while still keeping most of the forward facing momentum (along the Z axis) ?
Answer by Stratosome · Aug 14, 2018 at 10:06 AM
Hiya!
Are ya using a Rigidbody for the bounce? If so, if you want to totally remove movement on axes other than the Z axis, you can freeze its position of it on target axes under 'Constraints' in the inspector on the Rigidbody component.
If you don't want to totally remove the movement and just lessen it, what you could do is do something like:
Vector3 objVelocity = rb.velocity;
objVelocity.x /= 2.0f;
rb.velocity = objVelocity;
With something like this (maybe put in your FixedUpdate function), any velocity in the X direction is constantly cut in half until it is pretty much nothing. This way, the velocity will be mostly in the other directions. You can obviously modify how much you cut it by. Doesn't half to be half. Hopefully that gives you some ideas!
Thank you, I was curious how I can specifically change the velocity. Changing specific parts of the rb.velocity vector didn't do anything, but having set another Vector3 like you did in the example did the trick!
I'll put the code above in the method for collision handling, so that I keep some of the movement on those axes
Your answer
Follow this Question
Related Questions
RIGIDBODY HOW MAKE IT STOP BOUNCING HELP! 3 Answers
Rigidbody jump adds bounce energy to second jump 1 Answer
Ball bouncing up after rolling over a cliff when it should be rolling down? 0 Answers
Changing physics properties of a gameobject through code. 1 Answer
Re-Creating Unity's Friction and Bounce 0 Answers