0
votes

I am using Vector3.Lerp to move my Camera after my Player GameObject. Due to the mechanics of my game, the position of my Player GameObject can "teleport" and jump a couple of vectors, which changes the target position that the Camera is lerping towards.

This causes a stutter since the target position changes in the middle of the lerp, and causes the camera to jump/sutter due to the target position of the lerp being changed in the middle of the lerp.

How can I account for this, and make the camera smoothly move towards the new position"

2
why don't you use Vector3.MoveTowards(); ? - Tengku Fathullah

2 Answers

1
votes

For a a lerp, you pass it the start, the end, and a percentage to interpolate between.

It sounds like you are changing the end while in a lerp. What you would need to do is figure out what percentage the current camera position would be in the new start/end and update your percentage value to accommodate.

Ex:

Lerping between 0-10 with a current value of 5 would be a percentage of 0.5. If you change your end to 20, you now need to set your percentage to 0.25 to avoid stutter.

Another way to do this is to cheat and always lerp between current (instead of start) and target with a percentage of like 0.9 (adjusting as needed). This abuses lerp, but gives you an easing side effect that should be good enough if your positional updates are small.

0
votes

Instead of a normal A to B position lerp based on a percent between the two, you can use a set rate at which you move your camera towards the player.

cameraTransform.position = Vector3.Lerp(cameraTransform.position, player.position, Time.deltaTime * moveSpeed);

While this will not reach 1 (given moveSpeed is less than 1/deltaTime), it will smoothly move your camera towards the goal.