Godot 4.3
I want to calculate the vector represented by the white arrow in the picture. The objective is to point the object child of the Node3d (ItemHold) towards the Raycast. In simpler words, i want to point an object towards where the user is looking.
I tried doing this: getting the raycast position, getting the Node3d position, and using
look_at()
, but when running the code it points mostly correct, but not quite at the center. Here’s my code:
func get_raycast_position():
var space_state = get_world_3d().direct_space_state
var cam = get_viewport().get_camera_3d()
var mousepos = get_viewport().get_mouse_position()
var origin = cam.project_ray_origin(mousepos)
var end = origin + cam.project_ray_normal(mousepos) * RAY_LENGTH
var query = PhysicsRayQueryParameters3D.create(origin, end)
var result = space_state.intersect_ray(query)
return result.get("position", null)
func equip(itemHold: Node3D):
if itemHold != null and is_equippable and !is_equipped:
original_parent = get_parent()
var global_transform_before = global_transform
reparent(itemHold)
global_transform = global_transform_before
global_position = itemHold.global_position
var raycast_position = get_raycast_position()
if raycast_position != null:
look_at(raycast_position, Vector3.UP)
rotation.x = 90
freeze = true
is_equipped = true