Legacy Documentation: Version 4.6.2
Language: English
  • C#
  • JS
  • Boo

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

Sumbission failed

For some reason your suggested change could not be submitted. Please try again 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, hit: NavMeshHit, passableMask: int): bool;
public static bool Raycast(Vector3 sourcePosition, Vector3 targetPosition, NavMeshHit hit, int passableMask);
public static def Raycast(sourcePosition as Vector3, targetPosition as Vector3, hit as NavMeshHit, passableMask as int) as bool

Parameters

sourcePosition The origin of the ray.
targetPosition The end of the ray.
hit Holds the properties of the ray cast resulting location.
passableMask A mask specifying which NavMesh layers 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.

This function follows the path of a "ray" between the specified source and target positions on the navmesh. 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.

	var target: Transform;

function Update () { var hit: NavMeshHit; // Note the negative test condition! Using -1 for the mask // indicates all layers are to be used. if (!NavMesh.Raycast(transform.position, target.position, hit, -1)) { // Target is "visible" from our position. } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Transform target;
    void Update() {
        NavMeshHit hit;
        if (!NavMesh.Raycast(transform.position, target.position, out hit, -1)) {
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public target as Transform

	def Update() as void:
		hit as NavMeshHit
		if not NavMesh.Raycast(transform.position, target.position, , -1):
			pass