[C#] Vector2D.lerp questions

I have an object which is using Lerp2D, and I have a few questions

First, how do I make the transition smooth, without it being faster farther away and slower close up? The only solutions I’m seeing is using time.time, but after that turns past 1, the transition between the two objects is instant, which I don’t want, since the second object I have is moving. Unless there’s a better way than Lerp to make one object move towards another object that’s also moving?

Second, how do I “lerp” PAST an object. I actually want this object to aim for the other side of the object it’s on, because I want it to pass through it and then turn around and do the same thing in the opposite direction.

Here’s what I have so far, all in the Update() method.

        //Variables. Change "Target" to whatever the player/enemy should 
		var MyPos = new Vector2 (transform.position.x, transform.position.y);
		var EnemyPos = new Vector2 (xpos, ypos);
		var Target = GameObject.Find ("Cube");
                //Later, this will get a burst of speed when close to the target. That's what this is for.
		DisToTarg = Vector2.Distance (MyPos, EnemyPos);



		timer=timer+0.1f;

		Debug.Log (speed+"|"+DisToTarg);

		//Move to target
		transform.position=Vector2.Lerp (MyPos,new Vector2(EnemyPos.x+1, EnemyPos.y),0.1f*0.1f);//The Higher "speed" is the slower it travels. 0.1*0.1 if apparently the exact speed I need.

		//Look at target
		Vector3 dir = Target.transform.position - transform.position;
		float angle = Mathf.Atan2(-dir.x,dir.y) * Mathf.Rad2Deg;
		transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

		//Update position of target internally, with a few frames of delay. This is intentional.
		timer2++;
		if(timer2>=5){
			xpos = Target.transform.position.x;
			ypos = Target.transform.position.y;
			timer2=0;
		}

Also, note that everything is on a very small scale(I.E. all the objects are rather small.) I’ll fix that later.

Vector3.MoveTowards?

It’s 2D so… does that transfer over? Vector2 onstead?

same thing except without z axis,

transform.position=Vector2.MoveTowards(MyPos,new Vector2(EnemyPos.x+1, EnemyPos.y),Time.deltaTime * speed);

:slight_smile: Thanks! I’ll try that out.