How to use physics.raycast?

Um the docs just say to use “this.context.physics.raycast()” if i want to raycast, but is there any example? I have no idea how to get a list of hit objs from this function

Original Post on Discord

by user 263967078346653697

I believe it should look something along these lines:

import { RaycastOptions } from "@needle-tools/engine/src/engine/engine_physics";
import { Vector3, Ray } from "three";

...

const options = new RaycastOptions();
options.ray = new Ray(new Vector3(0,0,0), new Vector3(0,0,-1));

const results = this.context.physics.raycast(options);
results.forEach(result => {
    console.log(result.object.name);
});

you should set more things to the options like ignore mask or min max distance, not sure what are the default values => behaviour

If you want to raycast from e.g. the current mouse or touch pointer you dont need to input any options.

You can also give the options a screenspace position. Or like @kipash :cactus: showed give it the Ray explictly if you have a specific position in 3d space and direction

thank youu

by user 263967078346653697

tho i still cant get the console log to show up

by user 263967078346653697

i tried onn both trigger and non-trigger objects

by user 263967078346653697

image.png

by user 263967078346653697

this script is on my camera

by user 263967078346653697

“raycaster”

by user 263967078346653697

So you need to distinguish between raycasting against physics colliders and raycasting against any threejs object. raycast does raycast agains the threejs objects. raycastPhysicsFast does a simple raycast against the colliders in the scene.


okay i see, do i just slap some component on my object to turn it into threejs object?

by user 263967078346653697

Any 3d object in your scene is a threejs object. If it’s a mesh and not on layer 2 (IgnoreRaycast) in Unity it should be hit when you move the mouse over it in your example

i tried out the clicker thing

by user 263967078346653697

by user 263967078346653697

and my 3d object is clicking properly, so um they must be three.js objects

by user 263967078346653697

but still no luck on raycast

by user 263967078346653697

Everything in the 3d scene is threejs objects!

this.gameObject is a threejs Object3D