- Home /
Sphere overlaps vertical box collider and stutters
I have a fairly simple setup with a ball and some walls (pic1). The ball (sphere) can be controlled along the x-axis (left-right). I change the position with:
transform.position.x -= moveSpeed * Time.deltaTime;
transform.position.x += moveSpeed * Time.deltaTime;
I've also tried transform.Translate and at very low speed. Physics.gravity makes the ball fall up or down on the y-axis and hit the boxes below and above. Both gravity and movement work fine and collisions with the top and bottom boxes are nice and clean.
But when the sphere is moved into the vertical side wall it overlaps it and, if the movement key is held down, stutters back and forth in and out of the wall (pic2). This feels like really basic stuff but I cannot find anything to fix it!?
Answer by KeithK · Apr 05, 2011 at 08:06 PM
From the Physics documentation:
If you move the Transform of a non-Kinematic Rigidbody directly it may not collide correctly with other objects. Instead you should move a Rigidbody by applying forces and torque to it.
Instead of directly changing the Transform as you are. Try something like this:
rigidbody.AddForce(moveSpeed * Time.deltaTime, 0.0f, 0.0f, ForceMode.VelocityChange);
You will just have to negate the force for the different direction.
Yes! You're awesome. Changing it seems to have introduced a few other challenges, but it was certainly a big help and solved the stutter issue, so thanks!