Perform Raycasting
Cast rays to detect objects and hit points in your scene
Basic Raycasting
import { Behaviour } from "@needle-tools/engine";
import { Vector3 } from "three";
export class BasicRaycast extends Behaviour {
start() {
// Set ray origin and direction
const origin = new Vector3(0, 1, 0);
const direction = new Vector3(0, 0, -1); // Forward
// Cast ray using Needle's physics raycast
const hits = this.context.physics.raycast(origin, direction);
if (hits.length > 0) {
const hit = hits[0];
console.log("Hit object:", hit.object.name);
console.log("Hit point:", hit.point);
console.log("Distance:", hit.distance);
}
}
}Raycast from Camera
Raycast Options
RaycastOptions
Option
Type
Description
Line of Sight Check
Ground Detection
Raycast Against Specific Objects
Filter by Layer
Visualize Rays (Debug)
Intersection Data
Performance Tips
Use Needle's Built-in Raycast
Use Layers
Limit Ray Distance
Common Use Cases
Custom Grabbing/Picking
Projectile Path
Next Steps
Last updated