- Home /
Player movement with lerp is jerky.
Hey!I have a question! I've got this piece of code:
...
if (TargetChanged(Target, TarLastPos))
{
if(isLerping)
{
StopCoroutine("LerpPlayer");
}
StartCoroutine(LerpPlayer(0.5f));
}
}
//FUNCTIONS
bool TargetChanged(Transform target, Vector3 lastPos)
{
if (
//Target moved a tad
Mathf.Abs(target.position.x - lastPos.x) > 0.01f ||
Mathf.Abs(target.position.y - lastPos.y) > 0.01f ||
Mathf.Abs(target.position.z - lastPos.z) > 0.01f
)
{
return true;
}
else
{
return false;
}
}
//Move player so it is in fron of camera
IEnumerator LerpPlayer(float duration)
{
isLerping = true;
Vector3 StartingPos = Player.transform.position;
float StartTime = Time.time;
float MovementPercent = 0f;
while (MovementPercent < 1)
{
MovementPercent = (Time.time - StartTime) / duration;
this.transform.position = Vector3.Lerp(StartingPos, Target.position, MovementPercent);
yield return null;
}
isLerping = false;
this.transform.position = Target.position;
}
What it should do:It is meant to make the player's position get lerped to where the camera is pointing(indicated by the Vector3 Target) and because it is a Lerp, I need it to be in a IEnumerator,but that also means to not cause imense lag i set up a function to detect if the camera(Target) changed and so it stops the coroutine and start's it again, this time having the new Vectors.
What it does: It works...sort of. The player is "jolty" indicated in the gif, and I am not quite sure why, it should be smooth..
P.S. I couldn't find any question/answer that solved my problem yet, but I might have missed the answer i was looking for.
Gif: https://gyazo.com/0ade1715343fc82ce4a580359a7f4620
[preview of external content removed for GDPR compliance as it was including 3rd party cookies]
Ins$$anonymous$$d of yield return null
have you tried yield return new WaitForEndOfFrame()
or yield return new WaitForFixedUpdate()
, with the latter for when your object is physics-based?
Doesn't seem to help. With the EndOfFrame it doesn't lerp, and with FixedUpfate, well, you can look at the gif: https://gyazo.com/28a2541aa018ec4a21bbf24a30c451db
If you print out $$anonymous$$ovementPercent during the coroutine, does it ever break 1? You have it set up to break out once it's beyond 1, but the movement will still happen with your current ordering.
Answer by AmazingRuss · Jan 10, 2018 at 06:53 PM
Looks like you are running the camera and object in different update loops.
Try yielding on WaitForFixedUpdate instead.
If this is for mobile, yielding like that is a performance problem, and you'd be better off putting it in the FixedUpdate function.
It kinda works, I changed it from Late to Fixed and ditched the IEnumerable, and ins$$anonymous$$d of Lerp i used $$anonymous$$ove To.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Lock player movement within screen bounds 2 Answers
Moving player relative to non-child camera 1 Answer
Move player based on camera direction (MOBILE JOYSTICK) 0 Answers