I have asked this question on other community but still get no response after a month, so I decided to ask it here.
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?