返回介绍

Physics.SphereCast 球形投射

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

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

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

Parameters 参数

originThe center of the sphere at the start of the sweep.
扫描开始的球形中心点。
radiusThe radius of the sphere.
球的半径。
directionThe direction into which to sweep the sphere.
球形扫描的方向
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
如果返回真,hitInfo将获取更多的碰撞信息,参见:RaycastHit
distanceThe length of the sweep.
扫描的长度。
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 sphere sweep intersects any collider, otherwise false.

当球形扫描与任意碰撞器相交,返回true;否则返回false。

Description 描述

Casts a sphere along a ray and returns detailed information on what was hit.

沿着射线方向投射一个球形,并返回更多的碰到的信息。

This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way. Think of the sphere cast like a thick raycast. In this case the ray is specified by a start vector and a direction.

这可以用在光线投射无法满足要求时的情况下。例如你想找出一个特定大小的对象,比如角色能在某个地方移动,不与任何东西碰撞。球形投射就行有厚度的射线。在这种情况下射线是由开始向量和方向指定。

Notes: SphereCast will not detect colliders for which the sphere overlaps the collider. The sphere cast does not work against colliders configured as triggers. 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 SphereCast will hit the collider at it's new position.

注意,SphereCast不检测球体重叠的碰撞器。球形投射不检测触发器。如果从脚本或动画移动碰撞器,至少需要一个FixedUpdate来执行物理库更新数据结构,球形投射在它的新位置之前碰到碰撞器。

参见: Physics.SphereCastAll, Physics.CapsuleCast, Physics.Raycast, Rigidbody.SweepTest.

JavaScript:

#pragma strict
var charCtrl: CharacterController;
function Start() {
	charCtrl = GetComponent.<CharacterController>();
}
function Update() {
	var hit: RaycastHit;
	var p1: Vector3 = transform.position + charCtrl.center;
	var distanceToObstacle: float = 0;
	// to see if it is about to hit anything.
	if (Physics.SphereCast(p1, charCtrl.height / 2, transform.forward, hit, 10))
		distanceToObstacle = hit.distance;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
	CharacterController charCtrl;
 
	void Start() {
		charCtrl = GetComponent<CharacterController>();
	}
 
 
    void Update() {
        RaycastHit hit;
 
        Vector3 p1 = transform.position + charCtrl.center;
        float distanceToObstacle = 0;
 
        // Cast a sphere wrapping character controller 10 meters forward
        // to see if it is about to hit anything.
        if (Physics.SphereCast(p1, charCtrl.height / 2, transform.forward, out hit, 10))
        	distanceToObstacle = hit.distance;
 
    }
}

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

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

Parameters 参数

rayThe starting point and direction of the ray into which the sphere sweep is cast.
球形扫描的射线。
radiusThe radius of the sphere.
球的半径。
distanceThe length of the sweep.
扫描的长度。
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 sphere sweep intersects any collider, otherwise false.

当球形扫描与任意碰撞器相交,返回true;否则返回false。

Description 描述

Casts a sphere along a ray and returns detailed information on what was hit.

沿着射线方向投射一个球形,并返回更多的碰到的信息。

This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way. Think of the sphere cast like a thick raycast.

这可以用在光线投射无法满足要求时的情况下。例如你想找出一个特定大小的对象,比如角色能在某个地方移动,不与任何东西碰撞。球形投射就行有厚度的射线。在这种情况下射线是由开始向量和方向指定。

Note: The sphere cast does not work against colliders configured as triggers. 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 SphereCast will hit the collider at it's new position.

注意,SphereCast不检测球体重叠的碰撞器。球形投射不检测触发器。如果从脚本或动画移动碰撞器,至少需要一个FixedUpdate来执行物理库更新数据结构,球形投射在它的新位置之前碰到碰撞器。

参见: Physics.SphereCastAll, Physics.CapsuleCast, Physics.Raycast, Rigidbody.SweepTest.


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

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

Parameters 参数

rayThe starting point and direction of the ray into which the sphere sweep is cast.
带有开始点和方向的射线,用于球体扫描投射。
radiusThe radius of the sphere.
球体的半径。
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
hitInfo将获取更多的碰撞信息,参见:RaycastHit
maxDistanceThe max length of the cast.
layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Description 描述

Physics

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

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

发布评论

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