using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public class Follow : MonoBehaviour { public Transform targetToFollow; public Transform missionTarget; public Text text; public Text text1; public float lookAtRotationSpeed; public float moveSpeed; public float followRadius = 1.5f; public float fastRadius = 5f; 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 fromTarget = Vector3.ClampMagnitude(-lTargetDir.normalized, followRadius); 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); float speedBlend = Mathf.Clamp01((distance - followRadius) / (fastRadius - followRadius)); 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 class Follow : MonoBehaviour { public Transform targetToFollow; public Text text; public Text text1; public float lookAtRotationSpeed; public float moveSpeed; public float followRadius = 1.5f; public float fastRadius = 5f; 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 fromTarget = Vector3.ClampMagnitude(-lTargetDir.normalized, followRadius); Vector3 stopPoint = targetToFollow.position + fromTarget; // Compute a speed that's faster when far away and slower when close. float speedBlend = Mathf.Clamp01((distance - followRadius) / (fastRadius - followRadius)); 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.
