Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

NavMesh.Raycast

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function Raycast(sourcePosition: Vector3, targetPosition: Vector3, out hit: AI.NavMeshHit, areaMask: int): bool;
public static bool Raycast(Vector3 sourcePosition, Vector3 targetPosition, out AI.NavMeshHit hit, int areaMask);

Parameters

sourcePosition The origin of the ray.
targetPosition The end of the ray.
hit Holds the properties of the ray cast resulting location.
areaMask A bitfield mask specifying which NavMesh areas can be passed when tracing the ray.

Returns

bool True if the ray is terminated before reaching target position. Otherwise returns false.

Description

Trace a line between two points on the NavMesh.

The source and destination points are first mapped on the NavMesh, then a ray is traced from the source point towards the target. If the ray hits a NavMesh boundary, the function returns true and the hit data is filled. If the path from the source to target is unobstructed, the function returns false.

If the raycast terminates on an outer edge, hit.mask is 0; otherwise it contains the area mask of the blocking polygon.

This function can be used to check if an agent can walk unobstructed between two points on the NavMesh. For example if you character has an evasive dodge move which needs space, you can shoot a ray from the characters location to multiple directions to find a spot where the character can dodge to.

The NavMesh.Raycast is different from physics ray cast because it works on ???2.5D???, on the NavMesh. The difference to physics raycast is that the NavMesh version can detect all kind of navigation obstructions, such as holes in the ground, and it can also climb up slopes, if the area is navigable.

#pragma strict
// TargetReachable.cs
public class TargetReachable extends MonoBehaviour {
	public var target: Transform;
	private var hit: NavMeshHit;
	private var blocked: boolean = false;
	function Update() {
		blocked = NavMesh.Raycast(transform.position, target.position, hit, NavMesh.AllAreas);
		Debug.DrawLine(transform.position, target.position, blocked ? Color.red : Color.green);
		if (blocked)
			Debug.DrawRay(hit.position, Vector3.up, Color.red);
	}
}
// TargetReachable.cs
using UnityEngine;
using System.Collections;
public class TargetReachable : MonoBehaviour {
	public Transform target;
	private NavMeshHit hit;
	private bool blocked = false;
	void Update() {
		blocked = NavMesh.Raycast(transform.position, target.position, out hit, NavMesh.AllAreas);
		Debug.DrawLine(transform.position, target.position, blocked ? Color.red : Color.green);
		if (blocked)
			Debug.DrawRay(hit.position, Vector3.up, Color.red);
	}
}

If you are looking for to find the nearest point on the NavMesh you should use physics raycast to find a point in the world, see Move to Click Point example.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961