format
typescript
Physics.Raycast(ray, maxDistance, mask, cast_trigger)class: Physics
description
Performs a raycast.
This method casts a virtual ray in the scene to check for intersections with objects and stores information about the first object it intersects with.
parameter
| param_name | type | description |
|---|---|---|
| ray | Ray | The ray object to be cast. |
| maxDistance | number | The maximum distance the ray can be cast. |
| mask | number | The layers to perform collision detection on (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 ray:Ray;
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,1,0)
cube.transform.position = new Vector3(0,1,0);
// Create a ray that starts at (0,0,0) and points in the positive Y direction
this.ray = new Ray(new Vector3(0,0,0),new Vector3(0,1,0));
}
OnUpdate(): void {
// Perform a raycast
this.hit = Physics.Raycast(this.ray,10000,1);
Debug.Log(this.hit.point);
}
}