Search Unity

Locking quaternion on one axis?

Discussion in 'Scripting' started by Marcus_Nordstrom, Mar 4, 2021.

  1. Marcus_Nordstrom

    Marcus_Nordstrom

    Joined:
    Apr 20, 2017
    Posts:
    8
    Hello! :) i have this code: which rotates the rigidbody towards a gameobjects position. Bare in mind that i have to rotate the rigidbody on the Y-axis only and the camera on the X axis later...

    Code (CSharp):
    1. var qTo = Quaternion.LookRotation(test.transform.position - transform.position);
    2.             qTo = Quaternion.Slerp(transform.rotation, qTo, 10f * Time.deltaTime);
    3.            
    4.             _rigidbody.MoveRotation(qTo);
    But this does rotate the rigidbody on all axis (which is obvious)
    Now to the point.

    I have tried subtracting from the Quaternion or making a new quaternion like:

    new Quaternion(0, qTo.y, 0, qTo.w);

    But this gives weird results, as Quaternions are really weird to understand heheh :rolleyes:

    So how can i make it rotate on the Y-axis only?

    I do have rigidbody rotation constraints set on X and Z but the code seems to override this.
     
    roborodo likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
  3. Marcus_Nordstrom

    Marcus_Nordstrom

    Joined:
    Apr 20, 2017
    Posts:
    8
    Hmmm :confused: i don't understand :C

    Let me explain a little better as well:

    1. My game is first-person / FPS
    2. Rigidbody rotates on the Y axis only, to allow me to look left/right with the mouse
    3. Camera which is a child of the rigidbody rotates on the X-axis up and down with the mouse
    4. .....
    5. Now i need to snap to a specific point with my crosshair/aim in the world.
    6. I disable the mouse input so i cannot rotate with the mouse.
    7. Using Quaternion.Slerp gives me good results, BUT! when walking too close to the gameobject i snap my look to, i also rotate the whole rigidbody on all axis so it "lays down" on the ground as well, which i don't want o_O
     
    roborodo likes this.
  4. roborodo

    roborodo

    Joined:
    Jul 22, 2021
    Posts:
    2