Skip to content

format

typescript
Physics.CapsuleCast(p0, p1, radius, direction, maxDistance, mask, cast_trigger)

class: Physics

description

Perform a capsule cast in the scene.

This method casts a capsule-shaped ray in the scene to check for intersection with objects and stores the intersection information with the first object.

parameter

param_nametypedescription
p0Vector3One endpoint of the capsule.
p1Vector3The other endpoint of the capsule.
radiusnumberThe radius of the capsule.
directionVector3The direction in which the capsule is cast.
maxDistancenumberThe maximum distance the capsule is cast.
masknumberThe layer(s) to perform collision detection with (bitwise operation).
cast_triggerQueryTriggerInteraction?Whether the ray should perform collision detection with 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 p0 = new Vector3(0,1,0);
        let p1 = new Vector3(0,-1,0);
        let dire = new Vector3(0,1,0);
        // Perform a capsule cast
        this.hit = Physics.CapsuleCast(p0,p1,0.5,dire,10000,1);
        if (this.hit) {
            Debug.Log(this.hit.point);
        } else {
            Debug.Log("No object hit");
        }
    }

}