Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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.