返回介绍

Physics.Raycast 射线投射

发布于 2019-12-18 15:38:16 字数 12508 浏览 1250 评论 0 收藏 0

JavaScript => public static function Raycast(origin: Vector3, direction: Vector3, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;

C# => public static bool Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters 参数

originThe starting point of the ray in world coordinates.
在世界坐标,射线的起始点。
directionThe direction of the ray.
射线的方向。
distanceThe length of the ray.
射线的长度。
layermaskA Layer mask that is used to selectively ignore colliders when casting a ray.
投射射线,选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Returns 返回

bool True when the ray intersects any collider, otherwise false.

当光线投射与任何碰撞器交叉时为真,否则为假。

Description 描述

Casts a ray against all colliders in the scene.

在场景中投下可与所有碰撞器碰撞的一条光线。

JavaScript:

	function Update () {
		var fwd = transform.TransformDirection (Vector3.forward);
		if (Physics.Raycast (transform.position, fwd, 10)) {
			print ("There is something in front of the object!");
		}
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Update() {
        Vector3 fwd = transform.TransformDirection(Vector3.forward);
        if (Physics.Raycast(transform.position, fwd, 10))
            print("There is something in front of the object!");
 
    }
}

Notes: Raycasts will not detect colliders for which the raycast origin is inside the collider. If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it's data structures, before a Raycast will hit the collider at it's new position.

注意:如果射线从碰撞体的内部或者背面打,Raycast不检测碰撞。如果你用脚本或动画移动碰撞器,需要至少一个在FixedUpdate执行,因为物理库能更新它的数据结构,因此一条射线在它的新位置之前打到碰撞器。


JavaScript => public static function Raycast(origin: Vector3, direction: Vector3, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;

C# => public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters 参数

originThe starting point of the ray in world coordinates.
在世界坐标,射线的起始点。
directionThe direction of the ray.
射线的方向
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
hitInfo将包含碰到器碰撞的更多信息。
distanceThe length of the ray.
射线的长度。
layermaskA Layer mask that is used to selectively ignore colliders when casting a ray.
投射射线,选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Returns

bool True when the ray intersects any collider, otherwise false.

当光线投射与任何碰撞器交叉时为真,否则为假。

Description 描述

Casts a ray against all colliders in the scene and returns detailed information on what was hit.

在场景中投下可与所有碰撞器碰撞的一条光线,并返回碰撞的细节信息。

JavaScript:

	function Update () {
		var hit : RaycastHit;
		if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
			var distanceToGround = hit.distance;
		}
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Update() {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, -Vector3.up, out hit))
            float distanceToGround = hit.distance;
 
    }
}

另一个例子:

JavaScript:

	// Raycast up to 100 meters down
 
	function Update () {
		var hit : RaycastHit;
		if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
			var distanceToGround = hit.distance;
		}
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Update() {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F))
            float distanceToGround = hit.distance;
 
    }
}

JavaScript => public static function Raycast(ray: Ray, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;

C# => public static bool Raycast(Ray ray, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters 参数

rayThe starting point and direction of the ray.
射线的起点和方向
distanceThe length of the ray.
射线的长度。
layermaskA Layer mask that is used to selectively ignore colliders when casting a ray.
投射射线,选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Returns 返回

bool True when the ray intersects any collider, otherwise false.

当光线投射与任何碰撞器交叉时为真,否则为假。

Description 描述

Same as above using /ray.origin/ and /ray.direction/ instead of origin and direction.

使用ray.origin和ray.direction同上,替代origin和direction。

JavaScript:

function Update() {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
	if (Physics.Raycast (ray, 100)) {
		print ("Hit something");
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Update() {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, 100))
            print("Hit something");
 
    }
}

JavaScript => public static function Raycast(ray: Ray, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;

C# => public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters 参数

rayThe starting point and direction of the ray.
射线的起点和方向 。
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
hitInfo将包含碰到器碰撞的更多信息。
distanceThe length of the ray.
射线的长度。
layermaskA Layer mask that is used to selectively ignore colliders when casting a ray.
投射射线,选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Returns 返回

bool True when the ray intersects any collider, otherwise false.

当光线投射与任何碰撞器交叉时为真,否则为假。

Description 描述

Same as above using /ray.origin/ and /ray.direction/ instead of origin and direction.

使用ray.origin和ray.direction同上,替代origin和direction。

JavaScript:

function Update() {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	if (Physics.Raycast (ray, hit, 100)) {
		Debug.DrawLine (ray.origin, hit.point);
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Update() {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 100))
            Debug.DrawLine(ray.origin, hit.point);
 
    }
}

Physics

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

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

发布评论

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