Tyler Thompson
Tyler Thompson

Reputation: 59

Unity error when placing a random movement script on a sphere

I believe the NavMesh is not being placed in coordination with the sphere object would be my guess. This is the script the start of the script that I have on the sphere.

NavMeshAgent navMeshAgent;
    public float timeForNewPath;
    bool inCoRoutine;

    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();

        // Find a valid position on the NavMesh
        NavMeshHit hit;
        if (NavMesh.SamplePosition(transform.position, out hit, 1.0f, NavMesh.AllAreas))
        {
            // If a valid position was found, move the agent to that position
            navMeshAgent.Warp(hit.position);
        }
        else
        {
            Debug.LogError("No valid position on the NavMesh found close to the agent's starting position");
        }
    }

The else statement is always returned.

I am not sure what to do to get past this issue, any help is appreciated.

Upvotes: 0

Views: 32

Answers (1)

Insane One
Insane One

Reputation: 52

Looks like your object far away than 1.0f distance from the navmesh surface. You can try to increasing 1.0f to a bigger value, for example, 10.0f. It will find nearest position anyway.

Also, check, are you baked your NavMesh surface on the scene already? Sometimes it can be a problem :)

Upvotes: -1

Related Questions