- Home /
How to stop object from rotating during movement (Javascript)
I am working on a basic left/right/jump movement script in Unity. At first I used transform.translate to move the object around, but when the problem of going through colliders occured I started using a rigidbody, and the velocity function. But the problem with this is my object starts rotating uncontrollably from this. How can i make this script work?
function Update () {
if (Input.GetKeyDown ("space"))
rigidbody.velocity = Vector3.up;;
if (Input.GetKey ("left"))
rigidbody.velocity = Vector3.left ;;
if (Input.GetKey ("right"))
rigidbody.velocity = Vector3.right;;
}
I have looked all over for an answer to this, and although i know it is simple and probably easy to find I just can't figure it out.
In the Rigidbody component of the object you are moving, you can freeze rotation using the 'Constraints.'
Answer by LochandLoad · Nov 27, 2013 at 01:44 AM
Attach a Rigidbody to your object then make sure "Fixed Angle" is checked. This will stop it from rotating.