How to place an object at point where ray intersects another object?

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

I have the following code for projecting a ray from the camera and detecting any bodies that I intersect. I want to move the $hand scene to the point where the ray intersects:

mouse_pos = get_viewport().get_mouse_position()
ray_origin = $Rotator/Camera.project_ray_origin(mouse_pos)
ray_direction = $Rotator/Camera.project_ray_normal(mouse_pos)

from = ray_origin
to = ray_origin + ray_direction * 2
space_state = get_world().get_direct_space_state()
hit = space_state.intersect_ray(from, to)

if hit.size() != 0:
    $hand.translation = hit.position
	print(hit.position)
else:
	$hand.reset_hand()
    print($hand.translation)

However when I set the hand’s translation it disappears from view. How do I get that position of intersection and set another nodes 3D position to that point? Thank you!

:bust_in_silhouette: Reply From: jgodfrey

Just guessing because I don’t know what node-types you’re working with, but instead of…

$hand.translation = hit.position

… do you instead want …

$hand.position = hit.position

??

Thanks for taking the time, it wasn’t that, but I did find the correct answer which I’ve left and accepted.

stolen-biscuit | 2020-04-19 07:15

:bust_in_silhouette: Reply From: stolen-biscuit

So this was actually because $hand.translation is a local translation. I changed it to $hand.global_transform.origin = hit.position and that updated the global coordinates successfully