- Home /
Where should I Lerp (beginner).
I've been looking around and I've seen Lerp placed in Update and in LateUpdate. Trying to optimize my code but I'm new to this coding, Does it matter were I Lerp the position/rotation of a gameObject? Some guidance or a point in the right direction would be greatly appreciated.
The following code is from: http://www.cratesmith.com/archives/183 and http://answers.unity3d.com/questions/23566/how-to-get-an-object-to-follow-another-object-at-a.html
public void Update() {
// Just interpolate values each frame
currentTrajectory = SetTrajectory(Vector3.Lerp(currentTrajectory, updatedTrajectory,
Time.DeltaTime*trajectoryBlend); }function LateUpdate() { thisTransform.position.x = Mathf.Lerp( thisTransform.position.x, target.position.x + xOffset, Time.deltaTime smoothTime); thisTransform.position.y = Mathf.Lerp( thisTransform.position.y, target.position.y + yOffset, Time.deltaTime smoothTime);}
Here's a snippit of my code, its on the main camera.
void Update() {
appliedSpeed = Time.deltaTime * damp;
}
void LateUpdate()
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0,0,angle), appliedSpeed);
}
I split them up because I thought one could be calculated a bit less then the other. Not sure if this is a good thing or if I should keep them both in the update function.
The reason why I'm asking is because when I build my game to an Android device (Galaxy S2/S3) it'll be running smoothly but then the camera will cut/jerk in a random direction, the problem isn't present when the game builds on my PC and I've got no idea why its doing it :[ Halp...
[3]: http://answers.unity3d.com/users/17442/mox.html
Answer by Doireth · Feb 04, 2013 at 07:46 AM
It's best to make changes to camera (if it's tracking or following an object) in LateUpdate().
The reason for this is say for example you have a camera following a player, in Update() the player movements are calculated and performed, then in LateUpdate() the camera performs it's movement/changes based on the new position of the player.
That way you'll get smooth movement because if the camera's changes were also made in Update() there's no guarantee that they'll be accurate as the camera may be adjusted before the player, leading to jerky movement in the next frame as it's now trying to catch up.