Getting a continuous speed with Lerp

Hello, I’m using Lerp for my player movement and the issue is that it starts off fast and then slows down. I really don’t want this, I want a continuous movement from the player position to the position I select (raycasthit.point).

IEnumerator MovePlayer ()
	 {
		if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) 
	  {
			clickPos = hit.point;
		

		while (Vector3.Distance(clickPos, player.transform.position)>0.01f)
		{

			player.transform.position = Vector3.Lerp(player.transform.position,clickPos, Time.deltaTime * speed);
			yield return new WaitForEndOfFrame();
		}

	  }
			
	}

Maybe using Lerp isn’t the best way to move a player, but I just got it working :confused:

There are a couple problems here. I don’t know what your speed it, but for simplicity’s sake let’s say the result of Time.deltaTime * speed is roughly 0.5 all the time, your initial player.transform.position is (0,0,0) and your clickPos is (1,1,1).

Your first iteration of lerp will set the player.transform.position to be (0.5, 0.5, 0.5), halfway between these points.

Your second iteration of lerp will set the player.transform.position to be (0.75, 0.75, 0.75), which is halfway between (0.5, 0.5, 0.5) and (1,1,1). Time.deltaTime will bounce around, but your speed is presumably constant and so Lerp will always interpolate to a similar point. Lerp will keep moving your character halfway between its current point and the next point, making your character appear to slow.

Off the top of my head, you could try

Vector3 movementDelta = (clickPos - player.transform.position).Normalized * speed;

You create a normalized vector in the direction you want to move, then multiply it by your speed. In one second, your character will move movementDelta, so then movementDelta * Time.deltaTime is the character’s movement offset in that frame.