Search Unity

Rotation around local Y axis

Discussion in 'Scripting' started by Dark-Protocol, Sep 13, 2012.

  1. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Hello! I have a question that may seem a little basic.

    I have variable gravity and my player has to always face head upwards, feet down the direction of the gravity so if the gravity changes, the player rotation changes too.

    Code (csharp):
    1. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.FromToRotation(Vector3.up, -gravity), Time.deltaTime*10);

    Now, if I don't change the gravity and comment out this line, I had written that line of code that used to rotate my player:

    Code (csharp):
    1. transform.Rotate(transform.InverseTransformDirection(Vector3.up*Input.GetAxis("Mouse X")*2));
    As you can see, no matter how the player is rotated, it will always rotate the player around his local Y axis. However because of the previous line, this won't work because now the rotation gets set permanently all the time.

    How can I still have player-controlled local Y rotation if I have the gravity rotation all the time ?

    Thanks !
     
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Can you use transform.up in place of Vector3.up? I mean, I know you can, but does that get the results you're looking for?
     
  3. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    It wouldn't matter. I can use transform.up and InverseTransformDirection - they both work the same I guess.
    The problem is that Rotate function proves useless here, because every frame I set the rotation of the player to be equal to something.

    Thanks for the reply!
     
  4. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    try to use the Space.Self or Space.World to change the local or global rotation
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    we know our up, we know pretty much what we want our foward to be. So use cross products to reorient our forward:

    Code (csharp):
    1.  
    2.         var up = -Physics.gravity.normalized; //our desired up
    3.         var right = Vector3.Cross(up, this.transform.forward); //get our new right
    4.         if (right == Vector3.zero) right = Vector3.right;//in case up and forward were the same...
    5.         var forward = Vector3.Cross(right, up); //calculate our adjusted forward
    6.         this.transform.rotation = Quaternion.LookRotation(forward, up);
    7.  
     
  6. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    [EDIT] I think I've got it to work :)

    Hey! Thanks for the answers! The last one is pretty nice. I didn't think of that and though my version did the job for the gravity rotation, this is great, because it won't interfere with my local Y rotation.

    The last problem is how do I rotate the player around his Y axis? I want the player to control the Y axis rotation and if I use for example:

    Code (csharp):
    1.  
    2.       var up = -gravity.normalized;
    3.       var right = Vector3.Cross(up, this.transform.forward);
    4.       if (right == Vector3.zero) right = Vector3.right;//in case up and forward were the same...
    5.       var forward = Vector3.Cross(right, up); //calculate our adjusted forward
    6.       this.transform.rotation = Quaternion.LookRotation(forward, up);
    7.       transform.localEulerAngles.y += Input.GetAxis("Mouse X")*2;
    8.  
    It won't do the job because the rotation gets reset over and over again.
    Now what I think I can do is:

    have a variable:
    var yaw : float = 0.0f;

    have it set: yaw += Input.GetAxis("Mouse X")*2;

    And then do this:
    transform.localEulerAngles.y += yaw;

    Now I think this is going to work but my concern is that the value of yaw can reach huge numbers and I don't know if that's okay. Any ideas about that?

    Many thanks for the answers above once again :) !
     
    Last edited: Sep 14, 2012
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    no, it should work just fine... I was just adding to the localRotation yesterday for my game (I'm doing something similar... but instead of standing parallel with grav, I stand parallel to the terrain normal)