Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    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,220
    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. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Rigidbody setting in the inspector -> constranits -> rotation (x,y,z)
     
  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,220
    oh, yes, I know...
     
    Pryamus likes this.