Home Game Development unity – Set a minimal distance with navmeshagent

unity – Set a minimal distance with navmeshagent

0
unity – Set a minimal distance with navmeshagent

[ad_1]

After DMGregory’s remark, I’m rethinking my reply. You are already on a proper method, truly.

Let’s say another script assign goal (the participant) to your NavMeshAgent (enemy). It could be finished by means of public variable or by means of a way. As your goal is assigned, you wish to examine, what’s the distance left to it and whether it is smaller than some threshold (minDistanceSqr in instance under). If it’s – you wish to cease your agent from transferring. Otherwise – proceed following.

Now, if no concepts are current, this is a snippet that SHOULD (not examined) work. In your enemy script you wish to add:

public Transform goal;
public float minDistanceSqr;

non-public NavMeshAgent _agent;


non-public void Awake()
{
    _agent = GetComponent<NavMeshAgent>();
}

non-public void Update()
{
    if (goal != null)
    {
        var destPosition = goal.place;
        var sqrDistance = (rework.place - destPosition).sqrMagitude;

        _agent.vacation spot = destPosition;
        _agent.isStopped = (sqrDistance <= minDistanceSqr);
    }
    else
    {
        _agent.isStopped = true;
    }
}

As different reply recommend altering velocity to 0, it truly will not cease agent from calculating path to your goal, thus will probably be utilizing pointless CPU cycles.


Old (and unsuitable) reply (leaving for academic functions)

When setting goal place, simply add offset to the precise goal:

  1. calculate route out of your agent to desired goal and normalize it
    var offsetDirection = (participant.rework.place - enemy.rework.place).normalized;

  2. have a float sort variable which units the specified offset distance
    var offsetDistance = 1f;

  3. set vacation spot with offset:
    enemy.vacation spot = (participant.place + offsetDirection * offsetDistance);

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here