Skip to content

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_nametypedescription
centerVector3The center of the box.
extentsVector3The size of the box.
rotationQuaternionThe rotation of the box.
directionVector3The direction in which the box is cast.
maxDistancenumberThe maximum distance the box is cast.
masknumberThe layers to perform collision detection on (bitwise operation).
cast_triggerQueryTriggerInteraction?Specifies whether the query should hit triggers.

return

typedescription
RaycastHit|undefinedWhether 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");
        }
    }

}