0
\$\begingroup\$
using UnityEngine;
using UnityEditor;
using System.Collections;


public class SmoothFollow : MonoBehaviour
{
    public Transform missionTarget;
    public Transform targetToFollow;
    public float lookAtRotationSpeed;
    public float moveSpeed;
    public float followRadius = 1.5f;
    public float fastRadius = 5f;
    public float speedBoost = 0.5f;
    public bool follow = false;
    public bool reachTarget = false;
    public bool moveToMissionTarget = false;
    public float waitAtMission;

    private bool isChild = false;
    private bool changeToOrigin = false;
    private Vector3 lTargetDir;
    private float originSpeed;
    private float originFollowRadius;

    void Start()
    {
        originSpeed = moveSpeed;
        originFollowRadius = followRadius;

        if (reachTarget)
        {
            followRadius = 0.1f;
        }

        waitAtMission = Random.Range(3, 10);
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (follow)
        {
            Move(targetToFollow);

            var dist = Vector3.Distance(transform.position, targetToFollow.position);
            if (dist < 0.1f && isChild == false)
            {
                transform.parent = targetToFollow;
                transform.localPosition = new Vector3(0, 0, 0);
                transform.localRotation = Quaternion.identity;
                transform.localScale = new Vector3(0.001f, 0.001f, 0.001f);

                isChild = true;
                follow = false;
            }

            originSpeed = moveSpeed;
            originFollowRadius = followRadius;
        }

        if (moveToMissionTarget && missionTarget != null)
        {
            follow = false;
            Move(missionTarget);

            var distFromMissionTarget = Vector3.Distance(transform.position, missionTarget.position);
            if (distFromMissionTarget < 0.15f)
            {
                moveToMissionTarget = false;

                StartCoroutine(WaitAtMissionSide(waitAtMission));
            }
        }
    }

    private void Turn()
    {
        transform.rotation = Quaternion.RotateTowards(transform.rotation,
                Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);
    }

    private void Move(Transform Target)
    {
        if (reachTarget)
        {
            changeToOrigin = true;
            followRadius = 0.1f;
        }
        else if (changeToOrigin && reachTarget == false)
        {
            moveSpeed = originSpeed;
            followRadius = originFollowRadius;

            changeToOrigin = false;
        }

        lTargetDir = Target.position - transform.position;
        lTargetDir.y = 0.0f;

        Turn();

        float ms = moveSpeed;
        var distance = Vector3.Distance(transform.position, Target.position);
        // Compute a position no further than followRadius away from our target.
        Vector3 fromTarget = Vector3.ClampMagnitude(
            -lTargetDir.normalized, followRadius);
        Vector3 stopPoint = Target.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);
    }

    private IEnumerator WaitAtMissionSide(float WaitAtMissionTime)
    {
        yield return new WaitForSeconds(WaitAtMissionTime);

        follow = true;
    }
}

At this part :

var distFromMissionTarget = Vector3.Distance(transform.position, missionTarget.position);
            if (distFromMissionTarget < 0.15f)
            {
                moveToMissionTarget = false;

                StartCoroutine(WaitAtMissionSide(waitAtMission));
            }

The distance value on distFromMissionTarget is 1.000001 but if I want it to be less than 0.15f ?

\$\endgroup\$
1
\$\begingroup\$

your code needs cleaning a bit.

here is your problem:

Within function Move you are telling object to move to position stopPoint not to missionTarget.position. That's what object does, - it arrives to stopPoint.

and where is stopPoint? It is about 1 meters backwards from your missionTarget.position.

   Vector3 fromTarget = Vector3.ClampMagnitude(-lTargetDir.normalized,followRadius); // here you just created vector pointing 1 m backward
    Vector3 stopPoint = Target.position + fromTarget;  // and added target position with backwards pointing 1 m long vector...which gives you position 1 m backwards .. which results in stopping much earlier (about 1 m earlier..) 

P.S. note that any vector if normalized - its size becomes 1. So there is no benefit of normalizing vector if you are going to clamp it..

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .