Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tilt an object and level it back out.

Discussion in 'Scripting' started by alas-eridan, Aug 28, 2012.

  1. alas-eridan

    alas-eridan

    Joined:
    Aug 28, 2012
    Posts:
    3
    Hello, What I'm trying to achieve with my player controls (in a flying game) is to tilt the player side to side using mouse controls. I have done that successfully but my hang up is leveling the player back out. I need it to level out constantly and not just when the mouse has no input, it takes constant movement to tilt.

    I have already prototype this out in blender using python. Tilting was simply done by assigning the rotation value to an axis and then aligning the up axis to a vector:
    Code (csharp):
    1. player.alignAxisToVect([0.0,0.0,1.0], 2, 0.1)
    that is what axis to align, which vector, and over how much time.

    In the following code written in C# "tiltPoint" refers to an object that rotates with the player rig but does not rotate on the z-axis and "mainP" is the main parent of the rig which all the other movement is applied to (and what I want to align with, no need to restrict any axis). I'm using a Quaternion Slerp for rotation. As written it tilts the player just fine but still doesn't level out, I don't understand why.
    Code (csharp):
    1.  
    2. yRotation += Input.GetAxis("Mouse X");
    3.  
    4. //tilt the player
    5.         float oldTiltX = tiltPoint.transform.eulerAngles.x;
    6.         float oldTilty = tiltPoint.transform.eulerAngles.y;
    7.        
    8.         player.transform.rotation = Quaternion.Slerp(Quaternion.Euler(new Vector3(oldTiltX, oldTilty, yRotation * 5)),     mainP.transform.rotation, Time.deltaTime * 6);
    As I understand it "(Quaternion.Euler(new Vector3(oldTiltX, oldTilty, yRotation * 5)" should be my from and "mainP.transform.rotation" should be my to. Yet it never levels out but tilts just fine. Is there a better way to do this or am I making a simple mistake?

    Any help is appreciated!
     
  2. Lukas_GMC

    Lukas_GMC

    Joined:
    Aug 7, 2012
    Posts:
    40
    If I understood you correctly can't you simply have yRotation ease back to zero at the end of the frame?

    yRotation -= Mathf.Sign(yRotation) * Time.deltaTime * LEVEL_OUT_AMOUNT;
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Im more of a fan of rotatetowards... this will rotate from whatever to zero's (as an example)

    player.transform.rotation = Quaternion.RotateTowards(player.transform.rotation, Quarternion.Euler(0,0,0), 1);
     
  4. alas-eridan

    alas-eridan

    Joined:
    Aug 28, 2012
    Posts:
    3
    Solved!
    Lukas_GMC, you understood perfectly and where my logic failed you excelled. @JamesLeeNZ your suggestion seems like it should have worked and it's essentially what I was already trying (which I also thought should work). Maybe there's some problem because I'm already directly changing the rotation and doing it again doesn't register... For curiosities sake I'd like to know why it doesn't work. Because I'm already manipulating the rotation with my var "yRotation" it would make perfect sense to edit that value directly rather than try to alter the transform in two different ways.

    Thank you for the help! My player controls are now completed and I can move onto more complicated things.