返回介绍

Raycast 射线投射

发布于 2019-12-18 15:38:04 字数 3615 浏览 1347 评论 0 收藏 0

JavaScript => public static function Raycast(sourcePosition: Vector3, targetPosition: Vector3, out hit: NavMeshHit, areaMask: int): bool;
C# => public static bool Raycast(Vector3 sourcePosition, Vector3 targetPosition, out NavMeshHit hit, int areaMask);

Parameters 参数

sourcePositionThe origin of the ray.
射线的起点。
targetPositionThe end of the ray.
射线的终点。
hitHolds the properties of the ray cast resulting location.
保存射线投射位置的属性。
areaMaskA bitfield mask specifying which NavMesh areas can be passed when tracing the ray.
当绘制路径时,位掩码指定NavMesh区域中可以被通过的地方。

Returns 返回值

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

布尔类型 如果光线在到达目标位置之前终止返回true。否则返回false。

Description 描述

Trace a line between two points on the NavMesh.

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

起始点和目标点首先被映射在导航网格中,然后一条射线从起始点到目标点之间跟踪。如果射线击中一个导航网格边界,该函数返回true,hit数据被填满。如果从目标源的路径是通畅的,函数返回false。

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.

这个函数可以用来检测在NavMesh上一个agent是否可以在两点间通畅行走。例如:

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.

JavaScript:

#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);
	}
}

C#:

// 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);
	}
}

navmesh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文