代码片
。
public Ray (Vector3 origin, Vector3 direction);
沿着 direction 创建从 origin 开始的射线。
示例代码片
。
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
void Start()
{
// Create a ray from the transform position along the transform's z-axis
//从沿变换的z轴的变换位置创建射线
Ray ray = new Ray(transform.position, transform.forward);
}
}
代码片
。
public Vector3 GetPoint (float distance);
返回射线上 distance 个单位处的点。
代码片
。
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
Ray r = new Ray();
print(r.GetPoint(10)); // a point 10 units along the ray;沿射线指向10个单位
}
}
代码片
。
public string ToString ();
public string ToString (string format);
public string ToString (string format, IFormatProvider formatProvider);
返回此射线的格式化字符串。
format | 数字格式字符串。 |
---|---|
formatProvider | 一个指定区域性特定格式的对象。 |
代码片
。
public static bool Raycast (Vector3 origin, Vector3 direction, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);
origin | 射线在世界坐标系中的起点 |
---|---|
direction | 射线的方向。 |
maxDistance | 射线应检查碰撞的最大距离。 |
layerMask | 层遮罩,用于在投射射线时有选择地忽略碰撞体。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
bool 如果射线与任何碰撞体相交,返回 true,否则返回 false。
向场景中的所有碰撞体投射一条射线,该射线起点为 /origin/,朝向 /direction/,长度为 /maxDistance/。
您可以选择提供一个 LayerMask,以过滤掉不想生成与其碰撞的碰撞体。
您可以通过指定 queryTriggerInteraction 来控制是让触发碰撞体生成命中效果,还是使用全局 Physics.queriesHitTriggers 设置。
示例代码片
。
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
// See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries
void FixedUpdate()
{
// Bit shift the index of the layer (8) to get a bit mask
//位移图层(8)的索引以获得位掩码
int layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
//这只会将射线投射到第8层的碰撞器上。
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
//但是相反,我们希望与第8层以外的所有内容发生冲突。〜运算符执行此操作,它将反转位掩码
layerMask = ~layerMask;
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
//射线是否与除播放器层之外的任何对象相交
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit");
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
Debug.Log("Did not Hit");
}
}
}
代码片
。
public static bool Linecast (Vector3 start, Vector3 end, out RaycastHit hitInfo, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);
start | 起点。 |
---|---|
end | 终点。 |
hitInfo | 如果返回 true,则 hitInfo 将包含有关碰撞体的撞击位置的更多信息。 |
layerMask | 层遮罩,用于在投射射线时有选择地忽略碰撞体。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
如果有任何碰撞体与 start 和 end 之间的线相交,则返回 true。
示例代码片
。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform target;
void Update()
{
if (Physics.Linecast(transform.position, target.position))
{
Debug.Log("blocked");
}
}
}
代码片1
。
public static RaycastHit[] RaycastAll (Ray ray, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);
ray | 光线的起点和方向。 |
---|---|
maxDistance | 射线应检查碰撞的最大距离。 |
layerMask | 层遮罩,用于在投射射线时有选择地忽略碰撞体。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
代码片2
。
public static RaycastHit[] RaycastAll (Vector3 origin, Vector3 direction, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);
origin | 射线在世界坐标系中的起点 |
---|---|
direction | 射线的方向。 |
maxDistance | 射线应检查碰撞的最大距离。 |
layerMask | 层遮罩,用于在投射射线时有选择地忽略碰撞体。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
向场景中投射射线并返回所有命中对象。注意,这些结果的顺序未定义。
示例代码片
。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void Update()
{
RaycastHit[] hits;
hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
for (int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
Renderer rend = hit.transform.GetComponent<Renderer>();
if (rend)
{
// Change the material of all hit colliders
//更改所有命中对撞机的材质
// to use a transparent shader.
//使用透明着色器。
rend.material.shader = Shader.Find("Transparent/Diffuse");
Color tempColor = rend.material.color;
tempColor.a = 0.3F;
rend.material.color = tempColor;
}
}
}
}
代码片
。
public static bool BoxCast (Vector3 center, Vector3 halfExtents, Vector3 direction, out RaycastHit hitInfo, Quaternion orientation= Quaternion.identity, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);
ray | 光线的起点和方向。 |
---|---|
maxDistance | 射线应检查碰撞的最大距离。 |
layerMask | 层遮罩,用于在投射射线时有选择地忽略碰撞体。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
代码片2
。
public static RaycastHit[] RaycastAll (Vector3 origin, Vector3 direction, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);
center | 盒体的中心。 |
---|---|
halfExtents | 盒体各个维度大小的一半。 |
direction | 投射盒体的方向。 |
hitInfo | 如果返回 true,则 hitInfo 将包含有关碰撞体的撞击位置的更多信息 |
orientation | 盒体的旋转。 |
maxDistance | 投射的最大长度。 |
layerMask | 层遮罩,用于在投射胶囊体时有选择地忽略碰撞体。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
沿射线投射盒体并返回有关命中对象的详细信息。
示例代码片
。
//Attach this script to a GameObject. Make sure it has a Collider component by clicking the Add Component button. Then click Physics>Box Collider to attach a Box Collider component.
//将此脚本附加到GameObject。 单击“添加组件”按钮,确保它具有对撞机组件。 然后单击Physics> Box Collider附加Box Collider组件。
//This script creates a BoxCast in front of the GameObject and outputs a message if another Collider is hit with the Collider’s name.
//该脚本在GameObject的前面创建一个BoxCast,并在使用碰撞器的名称击中另一个碰撞器时输出一条消息。
//It also draws where the ray and BoxCast extends to. Just press the Gizmos button to see it in Play Mode.
//它还绘制了ray和BoxCast延伸到的位置。 只需按Gizmos按钮即可在“播放模式”中查看它。
//Make sure to have another GameObject with a Collider component for the BoxCast to collide with.
//确保BoxCast与另一个具有Collider组件的GameObject发生碰撞。
using UnityEngine;
public class Example : MonoBehaviour
{
float m_MaxDistance;
float m_Speed;
bool m_HitDetect;
Collider m_Collider;
RaycastHit m_Hit;
void Start()
{
//Choose the distance the Box can reach to
//选择Box可以达到的距离
m_MaxDistance = 300.0f;
m_Speed = 20.0f;
m_Collider = GetComponent<Collider>();
}
void Update()
{
//Simple movement in x and z axes
//在x和z轴上简单移动
float xAxis = Input.GetAxis("Horizontal") * m_Speed;
float zAxis = Input.GetAxis("Vertical") * m_Speed;
transform.Translate(new Vector3(xAxis, 0, zAxis));
}
void FixedUpdate()
{
//Test to see if there is a hit using a BoxCast
//使用BoxCast测试是否有匹配
//Calculate using the center of the GameObject's Collider(could also just use the GameObject's position), half the GameObject's size, the direction, the GameObject's rotation, and the maximum distance as variables.
//使用GameObject的对撞机的中心(也可以只使用GameObject的位置),GameObject的大小,方向,GameObject的旋转和最大距离的一半作为变量来计算。
//Also fetch the hit data
//同时获取匹配数据
m_HitDetect = Physics.BoxCast(m_Collider.bounds.center, transform.localScale, transform.forward, out m_Hit, transform.rotation, m_MaxDistance);
if (m_HitDetect)
{
//Output the name of the Collider your Box hit
//输出您的Box命中的对撞机的名称
Debug.Log("Hit : " + m_Hit.collider.name);
}
}
//Draw the BoxCast as a gizmo to show where it currently is testing. Click the Gizmos button to see this
//将BoxCast绘制为Gizmo,以显示其当前正在测试的位置。 单击Gizmos按钮以查看此内容
void OnDrawGizmos()
{
Gizmos.color = Color.red;
//Check if there has been a hit yet
//检查是否有点击
if (m_HitDetect)
{
//Draw a Ray forward from GameObject toward the hit
//从GameObject向着命中向前绘制一条光线
Gizmos.DrawRay(transform.position, transform.forward * m_Hit.distance);
//Draw a cube that extends to where the hit exists
//绘制一个立方体,该立方体延伸到命中所在的位置
Gizmos.DrawWireCube(transform.position + transform.forward * m_Hit.distance, transform.localScale);
}
//If there hasn't been a hit yet, draw the ray at the maximum distance
//如果还没有击中,请以最大距离绘制射线
else
{
//Draw a Ray forward from GameObject toward the maximum distance
//从GameObject向前朝最大距离绘制一条光线
Gizmos.DrawRay(transform.position, transform.forward * m_MaxDistance);
//Draw a cube at the maximum distance
//在最大距离处绘制一个立方体
Gizmos.DrawWireCube(transform.position + transform.forward * m_MaxDistance, transform.localScale);
}
}
}