- Home /
How to bounce a kinematic object back?
Hi,
My player object is a kinematic Rigidbody with no gravity. The object moves only left and right using the horizontal Input.GetAxis.
When position.x is greater than 20, I want the object to bounce back. The same thing for when position.x is greater than -20.
What I currently have is this:
if (currentTransform.position.x > 20) {
currentTransform.position = new Vector3(19, currentTransform.position.y, currentTransform.position.z);
}
This of course works, but it's a hard stop, not an elegant bounce. Furthermore, the player object starts stuttering when I reach the set border of 20, and I keep trying to move the object past it.
How do I create a bouncing effect and prevent the stuttering when the player holds the right or left key when the border is reached? Help would be much appreciated. Thank you.
Answer by Bunny83 · Jan 25, 2011 at 10:07 PM
Your stuttering comes from the 1.0 gab you have between your Limit value and your correction value.
if (currentTransform.position.x >= 20) {
currentTransform.position = new Vector3(20, currentTransform.position.y, currentTransform.position.z);
}
But why do you use a kinematic rigidbody? with a normal rigidbody you can specify a physicMaterial for the collider and the physicsystem make it bounce automatically. You have to move your object then via rigidbody. To create own natural looking bounce behaviour is not easy, that's why we have a physic engine ;)
I use a kinematic Rigidbody because the game is not designed to support physics. There is nothing that prevents the object from falling down. The only thing I need is to move the player object left and right, but with a limit of 20 units. Ins$$anonymous$$d of an abrupt stop, I would like it to bounce/bump against this imaginary border.