Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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,206
    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:
    718
    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,206
    oh, yes, I know...
     
    Pryamus likes this.