0
\$\begingroup\$

The speed value of the Nav Mesh Agent component doesn't seem to have any effect whatsoever on the speed of my game object. Actually none of the motion values seem to have any effect. I've attached photos of how I've set up my Nav Mesh Surface Plane and how I've set up the Nav Mesh Component attached to my game object. I followed an online tutorial to use the Nav Mesh Agent component via a C# script. The object moves randomly just fine except that the speed and other values on the Nav Mesh Agent component don't seem to do anything. You will notice that there is a commented out 'randSpeed' public float variable that I experimented with. Changing that variable also did not affect the speed.

Any ideas about this problem are appreciated. Thank you.

Nav Mesh Agent Component Settings

Nav Mesh Surface Component Settings

Unity API page for Nav Mesh Component settings

public class  RandomMovement2: MonoBehaviour
{
    UnityEngine.AI.NavMeshAgent navMeshAgent;
    public float timerForNewPath;
    private bool inCoroutine;
    //public float randSpeed;

    // Start is called before the first frame update
    void Start()
    {
        navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
        //randSpeed = navMeshAgent.speed;
    }

    Vector3 getNewRandomPosition()
    {
        float x = Random.Range(-40, 40);
        float z = Random.Range(-40, 40);

        Vector3 pos = new Vector3(x, 0, z);
        return pos;
    }

    IEnumerator doSomething()
    {
        inCoroutine = true;
        yield return new WaitForSeconds(timerForNewPath);
        GetNewPath();
        inCoroutine = false;
    }

    void GetNewPath()
    {
        navMeshAgent.SetDestination(getNewRandomPosition());
    }

    void Update()
    {
        if (!inCoroutine && (Reaction2.stopMoving==false))
        {
            StartCoroutine(doSomething());
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

You're problem is that your try to change a value-type object and expect to change something.

It's the same when you want to change the position. See this example:

// this wouldn't work
Vector3 position = gameObject.transform.position;
position = new Vector3(position.x + 1, position.y, position.z);

// this would work
Vector3 position = gameObject.transform.position;
position = new Vector3(position.x + 1, position.y, position.z);
gameObject.transform.position = position;

This is a kind of basic mistake but pretty common and lightly overseen.


Also you can change the speed value on a NavMeshAgent easily in the inspector and see the agent move faster. Just try it yourself.

\$\endgroup\$
0
\$\begingroup\$

Nevermind. I solved it.

The problem was the order in which I assigned the speed to the Nav Mesh Agent component in code. (The code that is commented out in the code above)

I had it written as randSpeed = navMeshAgent.speed;

It should have been navMeshAgent.speed = randSpeed;

Also, since it is in the Start function, make sure that you've set the speed before you hit start on the game. You could also put it in the Update function and then you can adjust it whenever you want during the game. And of course there are other ways in code you could configure it to update the speed during the game.


Although I still don't understand why we can't change the speed and other motion settings from directly within the Nav Mesh Agent component UI.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.