Question Rigidbody constrain

So I’ve made a simple move and jump code for my player, is there a way to make the player solid with no rotation? At the moment, if my player touches something, it can fall over and start rotating.

public class move : MonoBehaviour
{
    public float speed = 3.0f;
    private Rigidbody rb;
    public float jumpspeed = 10f;

    void Start()
    {
        rb = GetComponent<Rigidbody> ();
    }

    void Update ()
    {
        if (Input.GetKey (KeyCode.UpArrow))
        {
            Vector2 velocity = rb.velocity;
            velocity.y = jumpspeed;
            rb.velocity = velocity;
        }

        if (Input.GetKey (KeyCode.LeftArrow))
        {
            this.transform.Translate (new Vector3 (-speed * Time.deltaTime, 0, 0));
        }
        else if (Input.GetKey (KeyCode.RightArrow))
        {
            this.transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));
        }
      
    }

}

You can add constraints to the rigidbody (in code or in the Inspector) - that should prevent it from rotating. Unity - Scripting API: Rigidbody.constraints

Thanks! Is there a way to mark this post complete?

Nope, there is no “complete”, “correct”, or closing a thread (moderators excluded). They all just here for any future searchers/readers to browse. :slight_smile:

Saying “thanks” in reply to someone, indicating that it was solved for you might be inspiring enough for anyone in the future lol.

1 Like