- Home /
Lerp in LateUpdate while an animation is playing?
I recently started working on a top-down view game and I've made a character that moves similarly to a character in the mmo Dead Frontier (www.deadfrontier.com). I'm close but I have a problem that i can't figure out. The upper body and legs are animated separately, I got the body to point to the mouse in LateUpdate and the legs point in whatever direction you're moving in LateUpdate, but I want it to be a smooth transition instead of just snapping into position but I don't know enough about Quaternions to do this. I found a way to do it when there isn't an animation playing, but putting it in LateUpdate broke it. Here is the code for the legs:
function LateUpdate () {
if (Input.GetAxis("Vertical") > 0.1)
{
transform.eulerAngles = Vector3(180, 0, 0);
}
else if (Input.GetAxis("Vertical") < -0.1)
{
transform.eulerAngles = Vector3(180, 180, 0);
}
if (Input.GetAxis("Horizontal") > 0.1)
{
transform.eulerAngles = Vector3(180, 90, 0);
}
else if (Input.GetAxis("Horizontal") < -0.1)
{
transform.eulerAngles = Vector3(180, -90, 0);
}
if (Input.GetAxis("Horizontal") == 0.0 && Input.GetAxis("Vertical") == 0.0)
{
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist))
{
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
transform.Rotate(Vector3(180, 0, 0));
}
}
}
Why are you doing that on LateUpdate? You're not doing physics stuff and you're not using Time.fixedDeltaTime.
I'm doing it in LateUpdate because LateUpdate is called after the animation, if I put it in Update the animation messes it up and it won't turn at all. Sorry for the late reply, my internet has been acting difficult for the past few days.
ok, have a global variable holds the target rotation. and simply lerp to it in every frame. like this:
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed);
s.ta.c. I did what you said but the Slerp isn't working because it's in LateUpdate, it's still instant
Your answer
Follow this Question
Related Questions
Quaternion rotation not behaving as intended 1 Answer
Mirroring a Quaternion 2 Answers
Lerp or animate? 1 Answer
Smooth animation scrubbing with the mouse 3 Answers
Problem of creating smooth animations 3 Answers