Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

How do i stop cube from rotating while moving?

Discussion in 'Scripting' started by Pryamus, Dec 8, 2018.

  1. Pryamus

    Pryamus

    Joined:
    May 24, 2018
    Posts:
    10
    Hello everyone, i have managed to make my cube move with WASD and Space to jump but when it does move my cube like to flip and rotate, i don't want that to happen. How would i stop him from doing that?
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class playermovement : MonoBehaviour
    4. {
    5.  
    6.     public Rigidbody rb;
    7.     public float forwardForce = 50f;
    8.  
    9.     // Use this for initialization
    10.     private void Start()
    11.     {
    12.         Debug.Log("hello world");
    13.         rb = GetComponent<Rigidbody>();
    14.      
    15.     }
    16.  
    17.      private void FixedUpdate()
    18.     {
    19.         if (Input.GetKey(KeyCode.A))
    20.             rb.AddForce(Vector3.left * forwardForce);
    21.         if (Input.GetKey(KeyCode.D))
    22.             rb.AddForce(Vector3.right * forwardForce);
    23.         if (Input.GetKey(KeyCode.W))
    24.             rb.AddForce(Vector3.forward * forwardForce);
    25.         if (Input.GetKey(KeyCode.S))
    26.             rb.AddForce(Vector3.back * forwardForce);
    27.         if (Input.GetKey(KeyCode.Space))
    28.             rb.AddForce(Vector3.up * forwardForce);
    29.          
    30.        
    31.      
    32.  
    33.     }
    34. }
     
  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,208
    You must maybe consider to use transform.LocalEulerAngles in stead of AddForce. That will give you a more instant stop of rotation. You can also mess around with the rotational drag property of the rigidbody.
     
    Pryamus likes this.
  3. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,477
    Rigidbody setting in the inspector -> constranits -> rotation (x,y,z)
     
    Pryamus likes this.
  4. Pryamus

    Pryamus

    Joined:
    May 24, 2018
    Posts:
    10
    your A SUPER SAIYAN GOD.
     
  5. Pryamus

    Pryamus

    Joined:
    May 24, 2018
    Posts:
    10
    YOUR A SUPER SAIYAN GOD
     
  6. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,208
    oh, yes, I know...
     
    Pryamus likes this.