unity, NavMeshAgent agent;

introduce

NavMeshAgent is a component in Unity that can be used to navigate game objects on NavMesh. NavMesh is a triangular mesh used to represent walkable areas in a game scene. NavMeshAgent will automatically find the path closest to the target and move the game object along this path.


method

NavMeshAgent agent can be used to control the movement of game objects. You can control the movement speed, acceleration, rotation speed, etc. of the game object by setting the properties of NavMeshAgent. At the same time, you can also use the NavMeshAgent method to control the movement of the game object. For example, the SetDestination method can set the target position of the game object, and the Stop method can stop the movement of the game object.

NavMeshAgent agent = GetComponent(); can get the NavMeshAgent component on the current game object. The movement of game objects can be controlled through this component.

For example, the following code can be used to move a game object towards a target position:

NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.SetDestination(targetPosition);

This will make the NavMeshAgent automatically find the closest path to the target position and move the GameObject along that path. If you need to stop the movement of the game object, you can use the following code:

NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.Stop();

This will stop the game object from moving. It should be noted that after stopping, the target position needs to be reset in order for the game object to continue moving.

The NavMeshAgent component has many commonly used functions. Here are a few common functions:

SetDestination(Vector3 position):设置NavMeshAgent的目标位置。该函数会自动寻找距离目标位置最近的路径,并沿着该路径移动游戏对象。

Stop():停止NavMeshAgent的移动。该函数会立即停止游戏对象的移动,并清除当前的目标位置。

Resume():恢复NavMeshAgent的移动。该函数会使游戏对象继续沿着之前的路径移动。

SetPath(NavMeshPath path):设置NavMeshAgent的路径。该函数可以手动设置NavMeshAgent的路径,从而控制游戏对象的移动。

Warp(Vector3 position):将游戏对象瞬移到指定位置。该函数会立即将游戏对象传送到指定位置,而不需要经过寻路计算。

ResetPath():重置NavMeshAgent的路径。该函数会清除当前的路径,并停止游戏对象的移动。

CalculatePath(Vector3 targetPosition, NavMeshPath path):计算从当前位置到目标位置的路径
。该函数可以手动计算从当前位置到目标位置的路径,并将结果保存在NavMeshPath对象中。

for example

To track a player whose name is "player"

using UnityEngine;
using UnityEngine.AI;

public class ChasePlayer : MonoBehaviour
{
    
    
    private GameObject player;
    private NavMeshAgent agent;

    void Start()
    {
    
    
        // 获取玩家对象
        player = GameObject.FindWithTag("Player");

        // 获取NavMeshAgent组件
        agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
    
    
        // 如果玩家对象存在,则追踪玩家
        if (player != null)
        {
    
    
            // 设置NavMeshAgent的目标位置为玩家的位置
            agent.SetDestination(player.transform.position);
        }
    }
}

In the above script, we first use the GameObject.FindWithTag() method to get the player object named "player", and then use the GetComponent() method to get the NavMeshAgent component. In the Update() method, we use the agent.SetDestination() method to set the player's position as the NavMeshAgent's target position, so that the game object automatically tracks the player.

It should be noted that if the player object does not exist, NavMeshAgent will always look for the target position, causing the game object to keep moving. Therefore, in actual development, it is necessary to judge whether the player exists according to the specific situation, so as to avoid abnormal situations.


Guess you like

Origin blog.csdn.net/qq_20179331/article/details/130087382