Hey,
Im going in circles with such a basic concept. I am trying to raycast from an object in my scene, in a given direction vector, but my raycast is hitting objects that dont have colliders. I only want it to raycast against objects that have colliders.
I have referenced the documentation here and it says
Another option is to use the physics raycast methods which will only return hits with colliders in the scene.
using this const hit = this.context.physics.engine?.raycast();
I assume this is returning all ThreeJS objects, but the documentation says it should only return objects with colliders?
To implement a raycast that only interacts with objects that have colliders, you need to ensure you’re using the raycasting method properly. Based on the documentation you referenced, using this.context.physics.engine?.raycast() should return hits only with colliders.
Here are the steps you can follow:
Set Up Your Ray: You need to define your ray origin and direction. The ray should originate from the object in your scene you’re casting from.
Perform the Raycast:
Make sure you’ve specified the correct parameters for the raycast method, including the origin, direction, and any additional options.
Here’s an example of how you might write this in Needle Engine:
import { RaycastHit } from "@needle-tools/engine";
// Define your ray origin and direction
const rayOrigin = this.transform.position; // The object's position
const rayDirection = new THREE.Vector3(1, 0, 0); // Direction vector (modify as needed)
// Perform the raycast
const hit = this.context.physics.engine?.raycast(rayOrigin, rayDirection);
// Check if the ray hit any colliders
if (hit && hit.length > 0) {
const firstHit = hit[0]; // Access the first hit result
console.log('Hit Object:', firstHit.collider); // Log the collider that was hit
} else {
console.log('No hits detected against colliders.');
}
Make sure the objects you wish to raycast are added with colliders in your scene. If this.context.physics is not accessible, it may indicate that you’re not in the right context or lifecycle moment to access the physics engine. Confirm that your script is executed in the right phase.
You can refer to the API documentation for further details on the raycasting options if needed.
If you’re still having issues, please provide more details such as your specific setup or the context where the raycast is being called from.
Let me know if you need further assistance!
1 replies left - Get a PRO license for more AI replies. thread_JEeklVo2YwWvvd2V9C4NobI0 Reply to this message to continue the conversation
This line const hit = this.context.physics.raycast(raycastOrigin, raycastDirection, raycastDistance); returns an array of objects regardless of whether or not they have a collider. As soon as I try and access hit.collider.gameObject, or hit[0].collider.gameObject I get the error shown
I also see no raycasts drawn when I use the flag ‘debugraycasts’ so its hard to debug.
You need to use this.context.physics.engine.raycast(...)
Sorry for the confusion, the physics.raycast() method does do a “traditional” three.js raycast (with improvements for faster raycasting) but you want to use the physics engine raycast method (which uses rapier under the hood) and currently you use the wrong method
Just doing a quick test with this, when raycasting from a childed object, I dont see the ray. This script is attached to the large cube (not childed to anything) and also to the smaller cube (childed to the wheel), and the ray only draws from the unchilded cube.
export class RaycastTest extends Behaviour {
update(): void {
const rayDirection = new Vector3(0, -1, 0); // Direction vector (modify as needed)
// Perform the raycast
const hit = this.context.physics.engine?.raycast(this.gameObject.position, rayDirection, 10);
// Check if the ray hit any colliders
if (hit && hit.length > 0) {
const firstHit = hit[0]; // Access the first hit result
console.log('Hit Object:', firstHit.collider); // Log the collider that was hit
} else {
console.log('No hits detected against colliders.');
}
}
}
In the image, a ray should also be drawn from the small cube above the left wheel (Which is childed to the car). gameobject.position is the localPosition so unless Im misunderstanding, shouldnt that code be fine as is to cast the ray from?
Sorry I am spamming this thread because Ive wasted hours on what I thought would be the most trivial thing.
The code I posted above casts a ray but never detects a hit with any object. I dont know why. It casts a ray down (only on unchilded objects), and it never detects a hit on any object, whether it has a collider or not.
The third argument (10) is also not right i believe. See RapierPhysics | Needle Engine This is not a number but an object with options. Perhaps you get a javascript error in the console that you missed?