Quaternion Slerp Sanity Check

Been a long day here,

Does anyone see any glaring issues with this:

//Rotate the object based on the up axis of the target. (With Interpolation)
    transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(transform.forward,target.up),Time.deltaTime * 0.2f);

Thanks,

T

It depends on what exactly that line of code is meant to do for you, of course. If you’re just using this line of code to set the rotation of that transform once, then it might be just fine. But since you’re using Time.deltaTime, it indicates you’re trying to use this to animate the rotation change, maybe in a coroutine? If that is the case, then one thing that might be confusing is the fact that you’re using variables which change on every iteration to define the endpoints of the linear interpolation. You can use lerp that way, and sometimes it makes sense to do so, such as situations where you want the start-point to chase the result so that the animation eases to a stop instead of moving with uniform speed.

The second parameter is more troublesome. Note that you’re accessing transform.forward in that call to Quaternion.LookRotation when you’re defining the endpoint. But transform.forward changes every time the rotation is set. It’s the object’s local forward (the blue arrow) after all; it points in a different direction every time the rotation is changed. That’s gonna make the behavior of that Slerp’s endpoint really hard to understand. If it’s not behaving the way you expect, consider defining constant start- and endpoints and save them to variables outside the Slerp.