format
typescript
Physics.BoxCast(center, extents, rotation, direction, maxDistance, mask, cast_trigger)class: Physics
description
Performs a boxcast in the scene.
This method casts a box-shaped ray in the scene to check for intersection with objects, and stores the intersection information with the first object.
parameter
| param_name | type | description |
|---|---|---|
| center | Vector3 | The center of the box. |
| extents | Vector3 | The size of the box. |
| rotation | Quaternion | The rotation of the box. |
| direction | Vector3 | The direction in which the box is cast. |
| maxDistance | number | The maximum distance the box is cast. |
| mask | number | The layers to perform collision detection on (bitwise operation). |
| cast_trigger | QueryTriggerInteraction? | Specifies whether the query should hit triggers. |
return
| type | description |
|---|---|
RaycastHit|undefined | Whether the ray intersects with an object. |
code example
typeScript
class New_TypeScript
extends Component {
private hit:RaycastHit;
OnStart(): void {
// Create a cube in the scene
let cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
// Add a BoxCollider component to the cube
cube.AddComponent<BoxCollider>(BoxCollider);
// Set the position of the cube to (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);
// Perform a boxcast
this.hit = Physics.BoxCast(center,ext,rot,dire,1000,1);
if (this.hit) {
Debug.Log(this.hit.point);
} else {
Debug.Log("No object hit");
}
}
}