#NavMeshAgent troubles

1 messages · Page 1 of 1 (latest)

lofty iris
#

I'm using these bits of code: ```cs
Vector3 newPos = transform.position + dirToEnemy * 3;
GoToPoint(newPos);

public void RunState(bool run, Vector3 pos)
{
    Run = run;
    enemyToHumanDistance = Vector3.Distance(transform.position, pos);
    Debug.Log("Distance between human and enemy: " + enemyToHumanDistance);
    dirToEnemy = transform.position - pos;
} 

public void GoToPoint(Vector3 point)
{
agent.SetDestination(point);
}

           var nearestDist = viewDistance;
            Transform nearestEnemy = null;
            foreach (var enemy in enemyColliders)
            {
                if (Vector3.Distance(transform.position, enemy.transform.position) <= nearestDist)
                {
                    nearestDist = Vector3.Distance(transform.position, enemy.transform.position);
                    nearestEnemy = enemy.transform;
                    RunState(true, enemy.transform.position);
                }
            }```
robust pike
#

whats ur issue

lofty iris
#

I tried this: cs print("Human pos: " + transform.position + " Enemy position: " + pos + " dirToEnemy?: " + dirToEnemy);
which gave me these values: Human pos: (1.3, 1.1, 3.8) Enemy position: (-7.4, 0.1, 3.5) dirToEnemy?: (8.7, 1.0, 0.3)

lofty iris
#

I'm trying to stop it from going back and forth

#

I guess this could be done by only rotating it the necessary amount of rotation, although how do I accomplish that?