Object.layers is undefined when raycasting

When attempting to perform raycasting for collision detection, I’m getting an error that says “object.layers is undefined”.

private collision(dir: THREE.Vector3, amount: number) {
    const ray = new THREE.Raycaster(this.model.position, dir, 0, amount);
    const colliders = ray.intersectObjects(this.walls, false);
    return (colliders.length > 0 && colliders[0].distance - 0.5 < amount);
}

My model is of type THREE.Group<THREE.Object3DEventMap> | THREE.AnimationObjectGroup and the walls is an array of THREE.Object3D<THREE.Object3DEventMap>

I think the error occurs from this.walls
Please check if this.walls is true and test again.
And the second parameter of intersectObjects is recursive. You’re passing false, which means it won’t check children of the objects. If your walls have child objects that you want to include in the collision check, consider changing this to true.
This is just my opinion and I hope this will be helpful to your issue.

2 Likes