Home > database >  rotate without losing speed
rotate without losing speed

Time:09-15

i have a tank that is rotating toward the middlepoint of the camera i use a slerp. but it is slowing down if it almost there.

        private void RotateHull()
        {
            Quaternion target = Quaternion.Euler(0, _mainCamera.transform.eulerAngles.y, 0);
            hull.transform.rotation = Quaternion.Slerp(hull.transform.rotation, target, Time.deltaTime * turnTime);
            //SetRotate(hull, _mainCamera);
        }

this is the code. hull is the koepel en gunner.

CodePudding user response:

The usage of Lerp (or Slerp for that matter) is inappropriate here.

What you should use is Quaternion.RotateTowards, since that will allow you to rotate at a constant pace.

private void RotateHull()
{
    Quaternion target = Quaternion.Euler(0, _mainCamera.transform.eulerAngles.y, 0);
    hull.transform.rotation = Quaternion.RotateTowards(hull.transform.rotation, target, Time.deltaTime * turnTime);
}

CodePudding user response:

The behavior you are getting is a "smooth" behavior because you literally programmed it in that way. Also, the third parameter t is a percentage from 0f to 1f as you can se from the docs, so I really don't know how Time.deltaTime will affect the behaviour, but it is probably unwanted.

Consider a and b to be quaternions. If you do something like a = Slerp(a, b, 0.1) you will move it 10% until a reach close enough b. So you are moving just a part of the way, and it will become slower at end.

As in the time of this answer you did not specify what you want the code to do, you have three options:

  1. If you want a linear behavior, you should iterate the third parameter t from 0 to 1 in a given time. That will get rid of the smooth behavior. You can use a control variable or a Coroutine for that.

  2. You can increase the third t parameter to a fixed percentage (say 0.2f), removing deltaTime, and use some trial and error to get a satisfatory result.

  3. You can implement an "ease out" behaviour by increasing the value of the third parameter t over time, just remember to reset it when you start a new rotation.

  • Related