格式
typescript
Physics.BoxCast(center, extents, rotation, direction, maxDistance, mask, cast_trigger)所属类: Physics
描述
进行盒体射线检测。
该方法会在场景中发射一个盒体的射线,检测盒体与物体是否相交,并存储与第一个物体的相交信息。
参数
| 参数名 | 类型 | 描述 |
|---|---|---|
| center | Vector3 | 盒体的中心。 |
| extents | Vector3 | 盒体的范围 |
| rotation | Quaternion | 盒体的旋转。 |
| direction | Vector3 | 投射盒体的方向。 |
| maxDistance | number | 盒体投射的最大距离 |
| mask | number | 进行碰撞检测的层级(位运算) |
| cast_trigger | QueryTriggerInteraction? | 指定该查询是否应该命中触发器。 |
返回值
| 类型 | 描述 |
|---|---|
RaycastHit|undefined | 射线是否与物体相交 |
代码示例
typeScript
class New_TypeScript
extends Component {
private hit:RaycastHit;
OnStart(): void {
//在场景中创建一个立方体
let cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//为该立方体添加一个BoxCollider组件
cube.AddComponent<BoxCollider>(BoxCollider);
//将该立方体的位置设置为(0,2,0)
cube.transform.position = new Vector3(0,2,0);
}
OnUpdate(): void {
let center = new Vector3(0,0,0);
let ext = new Vector3(1,1,1);
let rot = new Quaternion(0,0,0,0);
let dire = new Vector3(0,1,0);
//进行盒体射线检测
this.hit = Physics.BoxCast(center,ext,rot,dire,1000,1);
if (this.hit) {
Debug.Log(this.hit.point);
} else {
Debug.Log("未击中物体");
}
}
}