weird vector3.lerp behavior

I have created a simple smooth follow type script for an orthographic 2d platformer.
At the beginning of each level I use the below code to snap the camera to the players transform.position and then follow him around using lerp. The problem is that the camera snaps to his position on the x and then slowly lerps to his position on the y as I stand still. I can’t for the life of me figure out why it doesn’t properly snap to his x,y

	Vector3 pos;
	float lerpPosition = 0.0F;
	float lerpTime = 300.0F;
	
	// Use this for initialization
	void Start () {
	    pos = GameObject.Find("Astronaut").transform.position;
	    transform.position = pos;
	}
	
	// Update is called once per frame
	void Update () {
		pos = GameObject.Find("Astronaut").transform.position;
		
		pos = new Vector3(pos.x, pos.y, -10);

		lerpPosition += Time.deltaTime/lerpTime;
		transform.position = Vector3.Lerp(transform.position,pos,lerpPosition);

		
	}

Hi,

you’re accumulating lerpPosition each frame, so the lerp value might get to high, resulting in an instant snap. If you want a linear movement, the lerp value should be constant. For debugging purpose, try adding a (tiny) static value as the lerp value and see if it works.

Besides that, calling GameObject.Find() each frame is a very bad idea performance wise. Instead, either add a public variable “Astronaut” and set the reference in the inspector, or store the reference to astronaut in a private variable inside Start(). In Update, use this variable instead of the costly call to GameObject.Find().

Even better, store it’s transform, as getting the transform costs as well (way not as much as GameObject.Find, but using the best practical optimization is always a good idea).