# 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. Dec 20, 2020 at 19:15