Raycast to center of screen

Hello! I need to shoot a raycast from a center of screen forward.

var from = this.camera.camera.screenToWorld(screenCenterX, screenCenterY, this.camera.camera.farClip);
          var to = this.camera.camera.screenToWorld(screenCenterX, screenCenterY, this.camera.camera.farClip);
          this.result = this.app.systems.rigidbody.raycastFirst(from, to);

I use this code. Am I right?

I raycasting to find an objects, that attached to entity, which attached to camera.

But when I found an entity, nothing happened. I move my entity with a device motion

screen center is var screenCenterX = this.app.graphicsDevice.width / 2;

Hi @pekarnik,

You need to definitely change the from var to start from the camera nearClip, otherwise from and to will be the same point. Basically no ray cast that way.

Change your first line to this:

var from = this.camera.camera.screenToWorld(screenCenterX, screenCenterY, this.camera.camera.nearClip);

Hi!
I see that raycast work now. But it work not when I find a object with device motion… only when I move to a center of entity, children of what is gameobjects with collision and rigidbody. what probably can I do wrong?

Sorry but I am not sure I am following this:

Can you elaborate how that should work?

image
This is an AR project, I move gameFrame with device motion, and when the camera look at element, I want to off a devicemotion.

Right now it’s work like this:


I see that entity picking not from center, or something wrong with collider
When u see [object Object] raycast found a target

I see, can you try updating your screen center coords to take into account the screen pixel depth:

var screenCenterX = (this.app.graphicsDevice.width / 2) * window.devicePixelRatio;
var screenCenterY = (this.app.graphicsDevice.height / 2) * window.devicePixelRatio;
1 Like

hmm, now it’s doesn’t work…

Basically you need to debug your screen width/height that is reported back from the graphicsDevice properties and try getting the center point (half of it).

In the video I can see it working, you just haven’t nailed yet the center of your screen.

1 Like

Thank you! I use

var screenCenterX = (this.app.graphicsDevice.width / 2) / window.devicePixelRatio;
var screenCenterY = (this.app.graphicsDevice.height / 2) / window.devicePixelRatio;

And change rigidbodies to kinematic mode, and all works fine now!

2 Likes