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. Dismiss Notice

Locking rotation on local aix

Discussion in 'Scripting' started by stanislav-osipov, Jul 22, 2013.

  1. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Hello.
    I need by physic body be able to rotate only by local X axis.

    Z axis should be fixed; (same as rigitbody rotation constraints but local)
    Y axis controlled by external script.

    This is solution I have so far

    Code (csharp):
    1.  
    2. Vector3 a = bike.transform.localRotation.eulerAngles;
    3.  
    4.         a.y = transform.localRotation.eulerAngles.y;       
    5.         a.z = 0f;
    6.  
    7.         bike.transform.localRotation =  Quaternion.Euler(a);
    8.  
    But problem, that "bike" not able to rotate by X axis only from 90 to 270. It not able to flip back. I'm not very good with Quaternion's that's why I asking fro help. Thanks!
     
    Last edited: Jul 22, 2013
  2. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Little bump

    I do not understand why unity does not allow rotation like 250, 0, 0 it modifies it to 290, 180, 180, but I have locked y, and z axis, and x rotation not able to be less than 270.

    I tried implement something like this.

    if(RoughlyEqual(a.z, 180f, 20f)) {
    f.z += 180f;
    f.y += 180f;
    }
    Now it able to be less then 270, but it has new issue it can simply stuck on x = 270. I do not know how to deal with it. I just have feeling that I do it wrong in first place. Thanks for reading.
     
    Last edited: Jul 23, 2013
  3. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    I really surprised that I can not find solution for simple problem..

    All I want to achieve is

    localRotation.eulerAngles.x - controlled by UnityPhysics
    localRotation.eulerAngles.y - controlled by me
    localRotation.eulerAngles.z - always zero.




    Current solution is looks like this, it can anyway can stuck on 270 or 90 degrees, but less frequently.
    Everything work as expected except this "stucking" issue
    Code (csharp):
    1.  
    2.  
    3.     private float lastZ = 0f;
    4.     private float lastX = 0f;
    5.  
    6.     public void FixRotation() {
    7.  
    8.  
    9.         Vector3 a = bike.eulerRotation;
    10.  
    11.         if(lastX == 270f) {
    12.             if(lastZ == 0f) {
    13.                 a.z = 180f;
    14.  
    15.             } else {
    16.                 a.z = 0f;
    17.             }
    18.  
    19.         //  a.x = 272f;
    20.  
    21.         }
    22.  
    23.         if(lastX == 90f) {
    24.             if(lastZ == 0f) {
    25.                 a.z = 180f;
    26.  
    27.             } else {
    28.                 a.z = 0f;
    29.             }
    30.         }
    31.  
    32.  
    33.         if (RoughlyEqual (a.z, 180f, 10f)) {
    34.             a.z = 180f;
    35.             a.y = transform.localRotation.eulerAngles.y;
    36.         } else {
    37.             a.z = 0f;
    38.             a.y = 180f +transform.localRotation.eulerAngles.y;
    39.         }
    40.  
    41.         lastZ = a.z;
    42.         lastX = a.x;
    43.    
    44.  
    45.         bike.transform.localRotation = Quaternion.Euler (a);
    46.    
    47.  
    48.     }
    49.  
     
  4. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    the thing with unity, converting that rotation for you it has to do with Gimble Lock, a serious pain for euler angles (transform rotation in unity's inspector).
    btw, you can lock both rotation axes from the rigidbody and still modify them by script i guess.
     
  5. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    That is not and option unfortunately.
    rigidbody is global, so after my bike goes in the first turn it will be complete mess with axes.
     
  6. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    you can add rigidbodies to the rest of your bike, and mark them as kinematic, and lock rotations, positions and such.
     
  7. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Event if rigidbodies inside other game object the constrains are global.

    It tried both of your solutions yesterday...:( Buy I really appreciate that you trying to help. Thanks!
     
  8. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    I have new Idea - rotate the track instead rotating bike.

    There is RotateAround function.

    But probably somebody can give quick tip, how to set rotation around point;

    I was trying something like this
    Code (csharp):
    1.  
    2. trackObject.transform.rotation = Quaternion.identity;
    3. trackObject.transform.RotateAround (bike.transform.position, Vector3.up, 60f);
    4.  
    Bur result is really weird)
     
    Last edited: Jul 24, 2013