- Home /
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
The only thing this code does is to slowly rotate about the object's local forward direction until its upper side aligns to the target's up direction - provided that the code is inside a periodic function like Update, FixedUpdate, LateUpdate, coroutine loop etc (like @CHPedersen said below). Is this what you want to do?
Yes, however it is not behaving as desired for some reason. I'm exa$$anonymous$$ing that second parameter under a microscope currently to see if that is the problem per @CHPedersen's advice.
Answer by CHPedersen · Sep 02, 2013 at 06:04 AM
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.
I think I understand what you are saying. So, my intent is to update the rotation of a camera based on the forward and up vector of the target automatically. Using Time.deltaTime * (x) where x is a speed, allows the interpolation to step in on itself as long as the first argument is the same as the variable that the Slerp is assigning to, correct?
Exactly. That's what I meant by having the start-point "chase the result". ;) What you said is just a different way to describe usage of Lerp, where you gradually decrease the range between start and stop in order to also decrease the step-length, i.e. slow down the animation.