Smooth rotate between changing rotations (already using Lerp, problems arise)
I've managed to sort of get smoothed rotations working for a Starfox clone using Slerp:
void Update () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
transform.position += (new Vector3(moveHorizontal, -moveVertical, 0) * speed * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(30f * moveVertical, 30f * moveHorizontal, 30f * -moveHorizontal), Time.time * rotSpeed);
}
This rotation code works alright-ish, but I'm having issues with the smoothness. When I jump from moving left to moving right, instead of rotating smoothly from -30f to 30f, the rotation instantly jumps to 0 and then begins rotating smoothly to 30f.
I've been looking around but all the tutorials and questions I could find just say "use slerp" or "use quaternions". I'm already doing that, and running into this issue. Is there maybe an alternate way to smooth rotations, or a way to get Slerp/Lerp to work with changing inputs?
Hmm. Slerp should work in this instance, although just be wary euler angles cannot be negative.
Are you sure I can't use negative numbers in Quaternion.Euler? The rotation works perfectly other than the fact that it snaps back to the original position if I try to reverse the direction of the rotation before it has a chance to smoothly return to the original position. This occurs in all axes of rotation.
The euler angle function automatically converts negatives to positives, however if you try modifying an euler angle to a negative directly, it will snap.
$$anonymous$$ore details: I tried connecting a joystick. Now, ins$$anonymous$$d of smoothly moving from rotation to rotation, the rotation seems to become a one-to-one match with the joystick rotation.
Essentially, when the moveHorizontal and moveVertical update, Slerp seems to give up and just instantly send the rotation to the destination regardless of the value of rotSpeed or Time.time.
This is ok-ish, but I would really like to limit the player's ability to rotate 60 degrees on the dot between frames.
Side note: you should be using Time.deltaTime for interpolation rather than Time.time. This is because the t value of a Lerp works on a scale of 0-1, and Time.time is the length in seconds since the beginning of the scene.