Search Unity

Rotation Issue

Discussion in 'Scripting' started by pete, Dec 30, 2006.

  1. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    yeah i'm stumped! i've got a rigidbody that smoothly rotates on all 3 axes from key input (think jet ski). i don't want my Y rotation to slerp back to zero because my vehicle needs to be steered around a circular track. i do want my X and Z to slerp to zero when a key is released. the closest i've been able to get is below but it doesn't work because this
    Code (csharp):
    1. currentRotation.y += targetRotation.y;
    doesn't know which side of 180 degrees it's on.

    sorry my code's not commented but hopefully it's pretty clear. probably won't get back here for a few days. so thanks in advance!

    Code (csharp):
    1.  
    2. function Update(){
    3.  
    4.     currentRotation = transform.rotation;
    5.  
    6.     targetRotation = Quaternion.Euler(Input.GetAxis("Vertical") * thrustAngle,  Input.GetAxis("Horizontal") * steeringAngle, Input.GetAxis("Horizontal") * ZtiltAngle);
    7.  
    8.     currentRotation.y += targetRotation.y;
    9.     currentRotation.x = targetRotation.x;
    10.     currentRotation.z = targetRotation.z;
    11.  
    12.     transform.rotation = Quaternion.Slerp(transform.rotation, currentRotation, Time.deltaTime * smooth);
    13.  
    14.     }
    15.  
     
  2. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    Have you tried doing it in euler angles and then convert it to quaterion in the end? You are doing sums directly on quaternions, don't know what to expect when you add two values of the quaterion (I think that the x/y/z don't have anything to do with your wold axis, I think that they represent - at least to some degree, you have to do some calculations - the vector around which your rotation is being done).
     
  3. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    yeah the code i posted isn't right at all. it *almost* works but don't try this at home kids! only meant it as an example of what i'm trying to do. it works to the point of everything rotates correctly except it gets stumped when it has to deal with 180 vs -180 (which i believe is expected behavior for this). everything else i tried either has the correct rotations but slerps Y to zero on key release or it ignores the X and Z rotation but rotates Y correctly.

    i did try some stuff with eulers but i let the script get really messy and i gave up on it. i was just hoping there's a one line magic bullet. you're probably right though. i'll take another look. thanks!
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    if you need to convert the angle to a certain range - say, -180 to 180 rather than 0 to 360 - you can do something that there is most certainly a computer science term for, but that I just refer to as "offset mod":
    Code (csharp):
    1.  
    2. angle = (angle + 180) % 360 - 180;
    3.  
     
  5. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    180 vs -180 vs 360 isn't the prob. thanks though! i think what nafonso said about the xyz in quats in relation to vector rather than world space is correct. so trying to do a sum didn't work because it doesn't do world space. i expected the result i got. i just haven't figured out how to slerp everything but keep my Y from returning to zero when the key is released.

    what i believe happens is when the horizontal key is released:
    Input.GetAxis("Horizontal") * steeringAngle = 0

    so i'm having a tough time keeping Y from going to zero because, well, i'm slerping to zero. i want my Y rotation to stay where it is when the key is released but i still want the rotation slerped while the key is down. now that i write it, maybe i just need to do a key down/up check. wouldn't that be a d'oh! :D

    anyway, i'll give it another look in a couple days. thanks for the help either way guys. talking it through always helps! :)
     
  6. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    currentRotation.eulerAngles.y += targetRotation.eulerAngles.y;
    currentRotation.eulerAngles.x = targetRotation.eulerAngles.x;
    currentRotation.eulerAngles.z = targetRotation.eulerAngles.z;

    simply changing those to eulers did it and much cleaner than my first attempt. thanks - that was an easy fix!