Version: 2023.2
言語: 日本語
public bool Raycast (Vector3 targetPosition, out AI.NavMeshHit hit);

パラメーター

targetPosition 期待する移動位置
hit ナビメッシュオブジェクトのヒット情報(ヒットした場合)

戻り値

bool エージェントとターゲット位置との間に障害物がある場合は true、そうでない場合は false

説明

エージェントを移動させずにナビメッシュ内の目的地に向かってのパスをトレースします

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. } } }