3D Caycast from mouse position with camera angle

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Itention

Hello,

as you can see on the pictures, the RayCast is only detected in the red area.

enter image description here
Is this because the camera angle is not included in the calculation?
The RayCast is a child of the Camera node and is pointing into the same direction of the camera in the editor.

Camera
enter image description here
How can I solve the problem in my code?

func _input(ev):
if ev is InputEventMouseMotion:
	var camera = get_parent()
	var from = camera.project_ray_origin(ev.position)
	var to = from + camera.project_ray_normal(ev.position) * ray_length
	
	raycast.translation = from
	raycast.cast_to = to
	
	var collision_point = raycast.get_collision_point ( ) 
	print(collision_point)

Thanks for your support.

So what you’re trying to do with raycasting?

Dlean Jeans | 2019-07-14 08:36

I am trying to pick up the cube with the mouse.

Itention | 2019-07-14 08:50

Try calling raycast.get_collider() which will give you the cube body.
Also RayCast.cast_to is relative to the raycast position so you probably don’t need to add from to to, too (lol).

Dlean Jeans | 2019-07-14 10:31

get_collider says [RigidBody:1136] but its colliding in the wrong area. (red area, see picture)
I removed from from to :)… but no changes… :frowning:

Maybe u were right:
After removing

raycast.translation = from

from the code everything works fine.

Very nice.

Itention | 2019-07-14 11:07