How to bounce a kinematic object back?


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.

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 ;)