public Ray(Vector3 origin, Vector3 direction);
射线:origin为起始点,direction为射线方向
public static bool Raycast(Ray ray);
物理射线监测:返回值为bool型,可以确定射线有无碰撞到碰撞体
public static bool Raycast(Ray ray, out RaycastHit hitInfo);
与上面类似,但能返回出来射线碰撞到的物体的物体信息,如:
public class RayTest : MonoBehaviour
{
public GameObject cube;
Ray myRay;
void Update()
{
myRay=new Ray(cube.transform.position,Vector3.forward);
RaycastHit hitInfo;
if(Physics.Raycast(myRay,out hitInfo))
{
Debug.Log("transform:"+hitInfo.transform.name+"point:"+hitInfo.point+"normal:"+hitInfo.normal);
}
}
}
可知:normal为碰撞到的点所在的面的法向量
RaycastHit是一个结构体,out出来的hitInfo包含:
public Collider collider { get; } 碰撞体
public float distance { get; set; } 碰撞体与射线起始点的距离
public Vector3 normal { get; set; } 碰撞点所在碰撞的法向量
public Vector3 point { get; set; } 碰撞点世界坐标系下的坐标
public Rigidbody rigidbody { get; } 碰撞到的物体的刚体
public Transform transform { get; } 碰撞到的物体的Transform组件
namespace UnityEngine
{
//
// 摘要:
// Structure used to get information back from a raycast.
[NativeHeaderAttribute("PhysicsScriptingClasses.h")]
[NativeHeaderAttribute("Modules/Physics/RaycastHit.h")]
[NativeHeaderAttribute("Runtime/Interfaces/IRaycast.h")]
[UsedByNativeCodeAttribute]
public struct RaycastHit
{
//
// 摘要:
// The Collider that was hit.
public Collider collider { get; }
//
// 摘要:
// Instance ID of the Collider that was hit.
public int colliderInstanceID { get; }
//
// 摘要:
// The impact point in world space where the ray hit the collider.
public Vector3 point { get; set; }
//
// 摘要:
// The normal of the surface the ray hit.
public Vector3 normal { get; set; }
//
// 摘要:
// The barycentric coordinate of the triangle we hit.
public Vector3 barycentricCoordinate { get; set; }
//
// 摘要:
// The distance from the ray's origin to the impact point.
public float distance { get; set; }
//
// 摘要:
// The index of the triangle that was hit.
public int triangleIndex { get; }
//
// 摘要:
// The uv texture coordinate at the collision location.
public Vector2 textureCoord { get; }
//
// 摘要:
// The secondary uv texture coordinate at the impact point.
public Vector2 textureCoord2 { get; }
//
// 摘要:
// The Transform of the rigidbody or collider that was hit.
public Transform transform { get; }
//
// 摘要:
// The Rigidbody of the collider that was hit. If the collider is not attached to
// a rigidbody then it is null.
public Rigidbody rigidbody { get; }
//
// 摘要:
// The ArticulationBody of the collider that was hit. If the collider is not attached
// to an articulation body then it is null.
public ArticulationBody articulationBody { get; }
//
// 摘要:
// The uv lightmap coordinate at the impact point.
public Vector2 lightmapCoord { get; }
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Use textureCoord2 instead. (UnityUpgradable) -> textureCoord2")]
public Vector2 textureCoord1 { get; }
}
}
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance);
与上面类似,但不是无限延伸的射线,最后一个参数规定了最大距离
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask);
这里的最后一个参数可以设置层级(int型),方法类似于我这一篇讲的Culling Mask
当然,也可以通过左移运算符来设置层级
左移运算符<<:左移表示乘以2,左移多少位表示乘以2的几次幂;
右移运算符>>:移动多少位表示除以2的几次幂。
if(Physics.Raycast(myRay,out hitInfo,100,1<<LayerMask.NameToLayer("UI")))
{
Debug.Log("transform:"+hitInfo.transform.name+"point:"+hitInfo.point+"normal:"+hitInfo.normal);
}
这样便可以检测UI层
位运算符:与(&)、非(~)、或(|)、异或(^)、<<(左移)、>>(右移)
想要检测多个层级可以:
if(Physics.Raycast(myRay,out hitInfo,100,1<<LayerMask.NameToLayer("UI")|1<<LayerMask.NameToLayer("Default")))
{
Debug.Log("transform:"+hitInfo.transform.name+"point:"+hitInfo.point+"normal:"+hitInfo.normal);
}
public static RaycastHit[] RaycastAll(Vector3 origin, Vector3 direction, [Internal.DefaultValue("Mathf.Infinity")] float maxDistance, [Internal.DefaultValue("DefaultRaycastLayers")] int layerMask, [Internal.DefaultValue("QueryTriggerInteraction.UseGlobal")] QueryTriggerInteraction queryTriggerInteraction);
检测多层,返回的是RaycastHit类型的数组
在应用方面,射线检测在3d射击游戏中,将屏幕坐标系转换成空间坐标系,可以直接:
Camera.main.ScreenPointToRay(Input.mousePosition);