Search Unity

Rotation Question. Problem with trying to rotate twice on individual axis. (Solved)

Discussion in 'Scripting' started by Jeremy-Borton, Oct 13, 2021.

  1. Jeremy-Borton

    Jeremy-Borton

    Joined:
    Oct 21, 2015
    Posts:
    63
    Hi, thanks for any help!

    a. This works:
    angles.z = -rotAngle; //(angles is stored in the start method angles = transform.rotation.eulerAngles;)
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(angles), Time.time * 1);

    or b. this works: transform.Rotate(Vector3.up, rotSpeed * Time.deltaTime, Space.World);

    but not both. Somehow a. overwrites b. even though b. comes after it in the code. I suppose it overwrites it on the next frame...

    The camera is top down.
    a. rotates my 3d spaceship around the z axis for making the ship look like it's banking in that direction.
    b. rotates my spaceship around the y axis for turning it with A and D keyboard keys.

    with both it only a. banks the spaceship and my turning is overwritten.

    I don't know how to fix it. The local up pivot of the spaceship changes when I rotate it on the z axis. I thought Space.World would rotate it on the world axis. And it works. But then it get's overwritten.
    How do I do both rotations without messing up the other?

    I think I'm going to have to combine the rotations both into one slerp rotate call or something.
     
    Last edited: Oct 13, 2021
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,429
    You might need to show more code, but in general, yes, you should not be trying to do both
    transform.rotation =
    and
    transform.Rotate()
    in the same frame, as they're both calculating new values for transform.rotation and will likely interfere with the effect you want. Rotating about two different axes can be pretty tricky to get right, since Euler Angles are applied in a specific order.

    This thread may help start the conversation about stacking Euler Angles, and gimbal lock, etc. https://forum.unity.com/threads/rot...spector-is-inconsistent-with-the-code.653575/
     
  3. Jeremy-Borton

    Jeremy-Borton

    Joined:
    Oct 21, 2015
    Posts:
    63
    I fixed it and got it to work! yay
    Thanks though!

    This was my solution:

    angles.z = -rotAngle;
    angles.y += rotSpeed * Mathf.Deg2Rad;
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(angles), Time.time * 1);

    I had to convert the degrees of the b. rotation to radians. And add it additively.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,429
    Great to hear. When folks figure out solutions to their questions, it can be helpful to the next person hitting Google if those solutions get added to the thread. Up to you.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,989
    This line:
    Doesn't make any sense ^^. First of all you're passing a changing start value into Slerp which completely changes the behaviour from being linear to being an accelerated exponential interpolation. Furthermore passing in Time.time as "t" also doesn't make much sense. The "t" value has be be in the range of 0 to 1. Time.time is the current game time which is ever increasing. So after the first second in the game the value will be larger than 1 so it's simply clamped to 1. So your line is essentially equal to simply:

    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(angles);
     
    Jeremy-Borton likes this.