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

Freezing Rotation

Discussion in 'Scripting' started by Kagedtiger, Jan 6, 2013.

  1. Kagedtiger

    Kagedtiger

    Joined:
    Sep 27, 2010
    Posts:
    76
    Hey all,

    I can't seem to get freezing rotation to work. I have a sphere with a rigidbody on it, and I have pressed the 'freeze rotation - z' checkbox on it.

    I want the sphere to be able to rotate in y for changing direction (I rotate it around the global up axis) and rotate along its own x (around axis transform.right) to roll forward. I *don't* want it to rotate along z, because then the transform.right axis moves and rolling forward no longer works properly.

    However, when I run my script, it seems like rotation along z is still happening without any change. Can anyone help me figure out why? Here is my control code:

    Code (csharp):
    1.  
    2.     void FixedUpdate () {
    3.        
    4.         //SPHERE MOVEMENT
    5.         float hor = Input.GetAxis("Horizontal");
    6.         float ver = Input.GetAxis("Vertical");
    7.        
    8.         //turning
    9.         if (Mathf.Abs(hor) > deadZone) {
    10.             rigidbody.AddTorque (Vector3.up * hor);
    11.         }
    12.        
    13.         //forward
    14.         if (Mathf.Abs(ver) > deadZone) {
    15.             //add torque around right-hand axis
    16.             rigidbody.AddTorque(transform.right.normalized * ver);         
    17.         }
    18.  
     
  2. Kagedtiger

    Kagedtiger

    Joined:
    Sep 27, 2010
    Posts:
    76
    Ah, I think I see the problem (although not the solution). I think freezing the x or z rotations freezes them in *world-space*, rather than local? Is that possible? The behavior seems to be that I can only roll forward (ie, rotate around the 'transform.right' axis) when I'm facing the appropriate direction, but not if I'm facing 90-degrees in another direction around the global up axis.

    Can I get it to freeze only the *local* x, y, or z, instead of global?
     
  3. VSZM

    VSZM

    Joined:
    Nov 18, 2013
    Posts:
    8