public bool Raycast (Vector3 targetPosition, out AI.NavMeshHit hit);

Parámetros

targetPositionThe desired end position of movement.
hitProperties of the obstacle detected by the ray (if any).

Valor de retorno

bool True if there is an obstacle between the agent and the target position, otherwise false.

Descripción

Trace a straight path towards a target postion in the NavMesh without moving the agent.

This function follows the path of a "ray" between the agent's position and the specified target position. If an obstruction is encountered along the line then a true value is returned and the position and other details of the obstructing object are stored in the hit parameter. This can be used to check if there is a clear shot or line of sight between a character and a target object. This function is preferable to the similar Physics.Raycast because the line tracing is performed in a simpler way using the navmesh and has a lower processing overhead.

using UnityEngine;
using UnityEngine.AI;

public class ExampleClass : MonoBehaviour { public Transform target; private NavMeshAgent agent;

void Start() { agent = GetComponent<NavMeshAgent>(); }

void Update() { NavMeshHit hit; if (!agent.Raycast(target.position, out hit)) { // Target is "visible" from our position. } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961