Smooth rotation on animation
Hi everybody !
I really need your help for that problem, i'm working on it since about 2 month now and i cant find how to make it work.
I simply do a bone rotation to aim a gun toward a target. So i do something like that :
void LateUpdate() {
spine.rotation = target.rotation;
}
Okay ? Nothing hard here, you will notice that i use LateUpdate to override the mecanim animations. And then it work perfectly... but not smoothly, so my problem is :
How to rotate the spine smoothly and keep overriding the mecanim animation ?
I already used something like :
void LateUpdate() {
spine.rotation = Quaternion.Slerp(spine.rotation, target.rotation, Time.deltaTime * 5f);
}
But it just dont work, the rotation is jittering.
Can i have some help with that ? Thank you so much !
Answer by ElDo · Oct 25, 2015 at 07:36 PM
I'm not sure on that but you should probably save your current spine Rotation in a separate variable and use this as your current Rotation instead of spine.Rotation. As you know you have to override the Mecanim stuff with LateUpdate. therefor your current spine Rotation won't be the same as you've set in the last LateUpdate. Hope you get what I mean but this is just a guess. Like this:
void LateUpdate() {
spine.rotation = Quaternion.Slerp(myRotation, target.rotation, Time.deltaTime * 5f);
myRotation=spine.rotation;
}
Thx for your help ElDo, this is exactly what i needed.
I added an offset and now i can aim the spine properly and smoothly
I validate your answer.
Here is my code if someone want to use it :
public void AimLook() {
Quaternion eulerOffset = Quaternion.Euler(manager.gun.ai$$anonymous$$gX, manager.gun.ai$$anonymous$$gY, manager.gun.ai$$anonymous$$gZ);
Ray ray = new Ray(transform.position, transform.forward);
Vector3 lookPosition = ray.GetPoint(hitDistance);
Quaternion targetRotation = Quaternion.LookRotation(lookPosition - manager.spine.position) * eulerOffset;
manager.spine.rotation = Quaternion.Slerp(spineRotation, targetRotation, Time.deltaTime * 5f);
spineRotation = manager.spine.rotation;
}
spineRotation just need to be set before with :
spineRotation = manager.spine.rotation;