# How to make the transform to slow down when he come from a far distance and getting close to the target?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

{
public Transform targetToFollow;
public Transform missionTarget;
public Text text;
public Text text1;
public float lookAtRotationSpeed;
public float moveSpeed;
public float speedBoost = 0.5f;

void Start()
{

}

// Update is called once per frame
void FixedUpdate()
{
Vector3 lTargetDir = targetToFollow.position - transform.position;
lTargetDir.y = 0.0f;
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);

float ms = moveSpeed;
var distance = Vector3.Distance(transform.position, targetToFollow.position);
text.text = "Transform Distance From Target " + distance.ToString();

// Compute a position no further than followRadius away from our target.
Vector3 stopPoint = targetToFollow.position + fromTarget;

// Move as far as we can at our speed ms to reach the stopPoint, without overshooting.
transform.position = Vector3.MoveTowards(transform.position, stopPoint, Time.deltaTime * ms);

moveSpeed += speedBlend * speedBoost;

text1.text = "Transform Speed " + ms.ToString();
}
}


I didn't understand how to work with the bottom part :

float speedBlend = Mathf.Clamp01((distance - followRadius) / (fastRadius - followRadius));
moveSpeed += speedBlend * speedBoost;


I tried to set here instead += to -= and then set it back to += and change the speedBoost here to be negative added the minus before it or in the inspector made it minus but then the transform is moving away from the target.

What I want to do is that if the transform when the game is starting is far from the target more then 5 for example the distance is 10 or 30 when the transform is getting close to the followRadius he just stop at once and instead I want it to slow down and stop at the follow Radius depending if the target is moving or not. If not moving slow down and stop at the follow radius if the target is moving slow down and move in the follow radius.

The main goal is to create a slow down effect when the transform is reaching the target from far.

This is what I tried according to the solution :

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

{
public Transform targetToFollow;
public Text text;
public Text text1;
public float lookAtRotationSpeed;
public float moveSpeed;
public float speedBoost = 0.5f;

void Start()
{

}

// Update is called once per frame
void FixedUpdate()
{
Vector3 lTargetDir = targetToFollow.position - transform.position;
lTargetDir.y = 0.0f;
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);

float ms = moveSpeed;
var distance = Vector3.Distance(transform.position, targetToFollow.position);
text.text = "Transform Distance From Target " + distance.ToString();
// Compute a position no further than followRadius away from our target.
Vector3 stopPoint = targetToFollow.position + fromTarget;

// Compute a speed that's faster when far away and slower when close.

ms = moveSpeed + speedBlend * speedBoost;

// Move as far as we can at our speed ms to reach the stopPoint, without overshooting.
transform.position = Vector3.MoveTowards(transform.position, stopPoint, Time.deltaTime * ms);
}
}


And the values in the inspector :

The main problem is that it's stuttering again. The code is in FixedUpdate.

If I'm settings the moseSpeed to 0 and the Speed Boost to 3 the transform is not moving at all. Only with this settings and I'm not sure if they are fine the transform is a bit slowing down when getting close to the target. I had to set the Fast Radius to 1.5 to make it slow down.

You copied the code I gave you incorrectly.

moveSpeed += speedBlend * speedBoost; is wrong.

You want what I wrote in my answer: ms = moveSpeed + speedBlend * speedBoost;.

This line should come before you try to move with that speed with MoveTowards:

    // Compute a position no further than followRadius away from our target.
Vector3 stopPoint = targetToFollow.position + fromTarget;

// Compute a speed that's faster when far away and slower when close.

ms = moveSpeed + speedBlend * speedBoost;

// Move as far as we can at our speed ms to reach the stopPoint, without overshooting.
transform.position = Vector3.MoveTowards(transform.position, stopPoint, Time.deltaTime * ms);


Once you write the code correctly, you can just set your moveSpeed parameter to a small value (maybe as low as zero) and your speedBoost parameter to a large value to get fast movement when far and slow movement when close.

Or you can reframe this as a lerp:

float slowestSpeed = 0;
float fastestSpeed = 5;

ms = Mathf.Lerp(slowestSpeed, fastestSpeed, speedBlend);

• I just edited my question with what I tried and what it's doing. For some reason it's stuttering again now. – Shamen Raze Dec 20 '20 at 19:15
• Do not make your fast radius and follow radius the same value. Fast radius should be larger than follow radius. In effect, you made a hard cutoff again, the very thing I keep telling you to stop doing. – DMGregory Dec 20 '20 at 19:16
• What values should I put in the inspector for make the distance from the target 1.5 as for follow and when the target stop and the transform is in the follow radius and stop too. Rhevalues now are : moveSpeed = 1.5 Follow Radius = 1.5 Fast Radius = 3 and Speed Boost also 3. Now it's mooving smooth with the slowdown but when it's following the target the distance is about 2.5-2.6 and when the target stop and the transform stop too the distance is : 1 I tried to play with it and can't get to 1.5 as follow radius and distance when both transform and target stop not moving. – Shamen Raze Dec 20 '20 at 23:20
• This is normal, because your player moves faster than your moveSpeed. So of you're moving at moveSpeed (the speed you have at the followRadius) you're moving too slow to keep up, and need to get a larger distance to ramp up the speed to match. You can increase the moveSpeed to match the player's speed, or change the beginning and ending ranges you use for calculating speedBlend so that you ramp up speed more sharply. – DMGregory Dec 20 '20 at 23:24