Why does my character controller slow down when aproaching destination

Hi All,

This is probably a very simple and obvious problem but I’m buggered if I can think of a solution.
As my character controller approaches it’s destination it slows up, like a linear ramp down.
I suspect it’s because my targetvdir gets smaller as it gets nearer the end. My question is what do I have to
do to my code example to make the controller move at a constant rate right to it’s destination?

 Vector3 targetvdir = TargetPosition - CurrentPosition;
    if(targetvdir != Vector3.zero)
    {
      Quaternion targetdir = Quaternion.LookRotation(targetvdir);
      CurrentRotation= Quaternion.Slerp(CurrentRotation, targetdir, 2 * Time.deltaTime); /* Rotate towards the target */
      CurrentRotation.eulerAngles = new Vector3(0, CurrentRotation.eulerAngles.y, 0);
    }
   
    Vector3 forwardDir = targetvdir;
    forwardDir = forwardDir * speed;

    controller.SimpleMove(forwardDir);

Try normalizing your forwardDir vector before applying the speed.

1 Like

Well that worked great thanks. Anychance you can explain as tho to a child what did the normalise do to the forwardDir?

Normalize just scales the points in a vector so that the distance from zero is one. (0, 0, 5) would normalize to (0, 0, 1). (4, 4, 0) would normalize to (I think) approximately (.71, .71, 0), etc. (Math gets harder from there and I’m lazy.)

What was happening was that your vector was always the distance between your current location and the target location. As you approach, the distance (obviously) gets smaller. You were using this distance as part of your speed.

Normalizing a vector removes the distance aspect of it, but leaves the direction intact.

Does that make sense?

Perfect sense thanks.