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_name | type | description |
|---|---|---|
| p0 | Vector3 | One endpoint of the capsule. |
| p1 | Vector3 | The other endpoint of the capsule. |
| radius | number | The radius of the capsule. |
| direction | Vector3 | The direction in which the capsule is cast. |
| maxDistance | number | The maximum distance the capsule is cast. |
| mask | number | The layer(s) to perform collision detection with (bitwise operation). |
| cast_trigger | QueryTriggerInteraction? | Whether the ray should perform collision detection with 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 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");
}
}
}