Consider this Unity C# script snippet attached to a GameObject with a NavMeshAgent component:
using UnityEngine;
using UnityEngine.AI;
public class SpeedTest : MonoBehaviour {
NavMeshAgent agent;
void Start() {
agent = GetComponent();
agent.speed = 5f;
agent.speed = agent.speed * 2;
Debug.Log(agent.speed);
}
} What will be printed in the console when the game starts?
using UnityEngine; using UnityEngine.AI; public class SpeedTest : MonoBehaviour { NavMeshAgent agent; void Start() { agent = GetComponent<NavMeshAgent>(); agent.speed = 5f; agent.speed = agent.speed * 2; Debug.Log(agent.speed); } }
Think about how the speed property is updated step-by-step.
The agent's speed is first set to 5, then doubled to 10. So the output is 10.
In Unity's NavMeshAgent component, which property determines how close the agent stops to its destination?
It is the distance from the target where the agent stops moving.
The stoppingDistance property sets how close the agent gets before stopping.
Look at this code snippet:
using UnityEngine;
using UnityEngine.AI;
public class MoveAgent : MonoBehaviour {
NavMeshAgent agent;
void Start() {
agent = GetComponent();
agent.destination = new Vector3(10, 0, 10);
}
void Update() {
if(agent.remainingDistance < 0.1f) {
Debug.Log("Arrived");
}
}
} The agent does not move at all. What is the most likely reason?
Check if the agent has a valid NavMesh to walk on.
If the NavMeshAgent has no NavMesh or is disabled, it cannot move even if destination is set.
Which of the following code snippets correctly sets the destination of a NavMeshAgent named agent to position (5, 0, 5)?
Remember the method used to assign a destination in NavMeshAgent.
The correct way is to call SetDestination() method with a Vector3 parameter.
You want to stop a NavMeshAgent instantly during gameplay. Which code snippet achieves this best?
Look for the property that pauses the agent's movement without disabling it.
Setting isStopped to true pauses the agent immediately without disabling the component or changing speed.