Hi, I am back again to ask for help with a little problem I have with my code.
I am trying to get the world position of my mouse instead of its viewport position, but anything I tried so far did not work.
I tried using the raycast method in the documentation but I'm not sure I understand how to implement it completely.
I want to have it as simple as I can but I did not want to add too many unnecessary steps like with the canvas method since I do not have a canvas yet. (I do not know how to make a UI yet and I prefer adding stuff in steps ')
Anyways, my question is:
Is there a way ,in 3D, I can use to transform my mouse position into world position from the _unhandled_input method without using a canvas?
My case is a little weird, since i am using a grid to place stuff in the world and would like to build to the closest grid position relative to the mouse's world position. That part I can figure out nicely I think, but it's the mouse world position that causes me problem '
I have this code currently:
It is far from good and I reverted back to the code that at least returned the viewport position rather than some weird float value that didn't line up.
func _get_position_from_click(mouse_pos):
var from = cam.project_ray_origin(mouse_pos)
var to = from + cam.project_ray_normal(mouse_pos) * ray_length
return to
func _unhandled_input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
print(event.position)
Huge thank you in advance for your help!