- Home /
Question by
Jake939 · Nov 27, 2013 at 08:33 PM ·
javascriptmovementrigidbodybasic
Object stops but then eventually breaks through
I am working on a movement code using the left and right keys, and the function rigidbody.MovePosition. When the player collides with walls it at first stops, but then if I hold down the key too long it will fall through the floor or walls eventually. The walls and floor have a box collider and the player has a rigidbody with default settings
function Update () {
if (Input.GetKey ("left"))
rigidbody.MovePosition(rigidbody.position + Vector3.left * 5 *Time.deltaTime);;
if (Input.GetKey ("right"))
rigidbody.MovePosition(rigidbody.position + Vector3.right *5 *Time.deltaTime`
Comment
Well moving objects can be done in multiple ways. Does your code depend on you using $$anonymous$$ovePosition? If not, just change to rigidbody.AddRelativeForce cause using forces provides better physics simulation. Read this
Answer by oneslyfox · Nov 27, 2013 at 09:41 PM
Use "FixedUpdate()" instead of "Update()" if you're dealing with an object with a Rigidbody.