Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Slerp ratio seems to react strangely

Discussion in 'Getting Started' started by steevilweevil, May 16, 2021.

  1. steevilweevil

    steevilweevil

    Joined:
    May 10, 2020
    Posts:
    1
    Hello, obligatory "I'm new to this".

    I'm building a space shooter game with a ship that moves in 3D. I've set my controls so that up and down control the pitch, left and right control the yaw, and it also rolls with left/right bumpers on my xbox controller. All good. Now, when it yaws from left to right, I wanted to introduce some tilt so it looks a bit more realistic. So I added a "tilt" script to the ship (with the main directional controls applied to a parent, so that the tilt just moves the ship relative to its parent without affecting the overall direction).

    If I set the tilt speed (which is passed as the ratio into the slerp function) to 1, then it functions fine but it tilts too fast. However virtually any other value sends the ship into an insane death spiral that doesn't stop even after I release the control, and it often rotates in different angles that shouldn't be controlled by the tilt script.

    What I want it to do is tilt slower, and correct whatever mistake I've made here!

    The script is just a short one:

    Code (CSharp):
    1. public class Tilt : MonoBehaviour
    2. {
    3.      public float tiltAngle = 45f;
    4.      public float tiltSpeed = 1f;
    5.      private Quaternion currentAngle;
    6.      private Quaternion targetAngle;
    7.  
    8.     void Update()
    9.     {
    10.           currentAngle = transform.rotation;
    11.           float tilt = tiltAngle * Input.GetAxis("Horizontal");
    12.           targetAngle = Quaternion.Euler(0f, 0f, -tilt);
    13.  
    14.           transform.localRotation = Quaternion.Slerp(currentAngle, targetAngle, tiltSpeed);
    15.  
    16.      }
    17.  
    18. }
    Thanks in advance.