Raycasting problem

I have a collider , just an empty entity.
I added collision and a rigid body on that entity.
When I use raycasting, if the entity collider has a small (normal) size (1,1,1) , for some reason i can’t detect it with ray.
If i use bigger , i can. But the problem is it also detect anywhere on the screen.
Is this a bug , because it was working 30 days ago

Here are screenshots

So, couple of things.

  1. [Redacted]

  2. If you move your camera on mouse move, and from your method name I would assume you also cast a ray to test the collision, then your collision tests will only run when you move your mouse. If your mouse is still and not moving, no rays will be cast. Instead, you want to put it under .postUpdate() method with proper guards to prevent the casts when not needed, like your this.chipTransform. Casting it from .postUpdate() vs .update() will make sure that the camera has moved to the new position before starting the cast.

If this still doesn’t help, make a small project, with a simple collider and a console log, that reproduces your problem.

Is the camera orthographic or perspective? If it’s orthographic, the from position needs to be calculated from a screenToWorld function call with the nearClip as the cameraZ parameter.

You are right, but I do not understand why.
If I am shooting the ray to mouse direction, why ray is going only when the collider is in the middle
It doesn’t make any sense to me

The camera is in orthographic mode, and this is the code that is the same as I can see with your suggestion
this.entity.camera.screenToWorld(mouseEvent.x, mouseEvent.y, this.entity.camera.nearClip);
Still not working, when i point to any collider.
It’s detecting the collider only when it is in the middle of the screen

Are you using the nearClip for the ‘from’ position and farClip for the ‘to’ position?

var from = this.entity.camera.screenToWorld(mouseEvent.x, mouseEvent.y, this.entity.camera.nearClip);
var to = this.entity.camera.screenToWorld(mouseEvent.x, mouseEvent.y, this.entity.camera.farClip);
1 Like

It’s working now, thank you so much @yaustar
The PlayCanvas community is really something <3

1 Like

@Dava sorry, I misread your code, you are using the screen coordinates from the mouse event correctly.