- Home /
Unity 2d - Vector3.Lerp Not Working as Expected
I am trying to put player position to the middle of camera when it is reaching near the edge of the camera. It works correctly with the following code:
transform.position = playerPosition;
where playerPosition
is the current position of the player.
But, when I try it with Vector3.Lerp or Vector3.MoveTowards, it won't reach the player's x position.
Here is how it looks when using Lerp or MoveTowards
https://www.youtube.com/watch?v=E50RpycrUxI
And here is when not using Lerp or MoveTowards
https://www.youtube.com/watch?v=ng_A4AfDQFE
The script:
public GameObject player;
private Vector3 playerPosition;
private Vector3 pos;
public float offsetSmoothness;
void Update () {
// player's position relative to camera
pos = Camera.main.WorldToViewportPoint(player.transform.position);
playerPosition = new Vector3(player.transform.position.x,
transform.position.y,
transform.position.z);
// if player reach the edge of the camera do the following
if (pos.x < 0.1 || pos.x > 0.9)
{
transform.position = Vector3.Lerp (transform.position,
playerPosition,
offsetSmoothness * Time.deltaTime);
// transform.position = playerPosition;
}
}
Why Vector3.Lerp is not positioning the player to the center of the camera when this condition is used to execute it: if (pos.x < 0.1 || pos.x > 0.9)
?
And how to make it working as expected, which is making the Vector.Lerp position the player to the center of camera when the player close to the edge of the camera?
Your answer
![](https://koobas.hobune.stream/wayback/20220612145957im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Player GameObject changes position when I start the game 4 Answers
Mathf.Lerp not working 2 Answers
2D RPG boomerang instantiation help. 2 Answers