Hi
I am moving a certain object to the target with a certain speed using Coroutine.
What I want is for the object to slow down when it comes to a certain distance from the target, but still reach the target.
I just need to implement “current speed” and “remaning distance” paremeters or something else to here:
float distance2 = Vector3.Distance(go.transform.position, TargetPosition.Value);
if (distance2 < 5.0f)
{
speed = Mathf.MoveTowards(remainingDistance, 0.0f, Time.deltaTime);
}
For an example, when i added this line (* 2.5f) to this code, it works great and it stops smooth. (if my projectile speed is 15)
float distance2 = Vector3.Distance(go.transform.position, TargetPosition.Value);
if (distance2 < 5.0f)
{
speed = Mathf.MoveTowards(remainingDistance * 2.5f, 0.0f, Time.deltaTime);
}
with *2.5f: ( speed is 15)

without *2.5f: ( speed is 15)

However, When i change the projectile speed, it doesn’t stop smooth again.
with *2.5f: ( speed is 5)

So I need to change this *2.5f value dynamically. But I don’t know how…
My whole Coroutine:
public IEnumerator GotoPosition()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
Vector3 startPosition = go.transform.position;
Vector2 Noise = new Vector2(Random.Range(MinNoise.x, MaxNoise.x), Random.Range(MinNoise.y, MaxNoise.y));
Vector3 BulletDirectionVector = new Vector3(TargetPosition.Value.x, TargetPosition.Value.y + yOffset, TargetPosition.Value.z) - startPosition;
Vector3 HorizontalNoiseVector = Vector3.Cross(BulletDirectionVector, Vector3.up).normalized;
float distance = Vector3.Distance(go.transform.position, TargetPosition.Value);
float remainingDistance = distance;
while (remainingDistance > disableFollowDistance)
{
if (smoothEnd.Value)
{
float distance2 = Vector3.Distance(go.transform.position, TargetPosition.Value);
if (distance2 < 5.0f)
{
speed = Mathf.MoveTowards(remainingDistance * 2.5f, 0.0f, Time.deltaTime);
}
else
{
speed = MoveSpeed.Value;
}
}
NoisePosition = NoiseCurve.curve.Evaluate(1 - (remainingDistance / distance));
if (followTargetBool)
{
if (useCurve)
{
go.transform.position = Vector3.Lerp(startPosition, TargetPosition.Value + new Vector3(0, yOffset, 0), 1 - (remainingDistance / distance)) + new Vector3(HorizontalNoiseVector.x * NoisePosition * Noise.x, NoisePosition * Noise.y, NoisePosition * HorizontalNoiseVector.z * Noise.x);
}
else
{
go.transform.position = Vector3.Lerp(startPosition, TargetPosition.Value + new Vector3(0, yOffset, 0), 1 - (remainingDistance / distance));
}
lookVector = (lastPosition - go.transform.position).normalized / Time.fixedDeltaTime;
lastPosition = go.transform.position;
go.transform.rotation = Quaternion.LookRotation(-lookVector);
}
else
{
if (useCurve)
{
go.transform.position = Vector3.Lerp(startPosition, reachPos + new Vector3(0, yOffset, 0), 1 - (remainingDistance / distance)) + new Vector3(HorizontalNoiseVector.x * NoisePosition * Noise.x, NoisePosition * Noise.y, NoisePosition * HorizontalNoiseVector.z * Noise.x);
}
else
{
go.transform.position = Vector3.Lerp(startPosition, reachPos + new Vector3(0, yOffset, 0), 1 - (remainingDistance / distance));
}
lookVector = (lastPosition - go.transform.position).normalized / Time.fixedDeltaTime;
lastPosition = go.transform.position;
go.transform.rotation = Quaternion.LookRotation(-lookVector);
}
remainingDistance -= speed * Time.deltaTime;
yield return null;
}
stopCurrent();
}
Please don’t suggest any 3rd party asset.
Thanks for help.