If the projection is changed from “Perspective” to “Orthogonal,” the current code would not work for picking the turret position. After some digging around I find that if the code is changed to:
then it works. However there’s still an offset along the z axis for some reason. This is more apparent if the multiplier is changed to 1000. Am I going about this the right way? Is there a way to get rid of the offset for good?
P.S. If you keep the current camera settings, later on in the course you’ll find that the trees near the edge of the screen will look distorted. This can be remedied by lowering the FOV or using Keep Width instead of Keep Height.
Not something ive ever played with using an ortho camera in this way. I’m going to have a look at this a little more tonight and get back.
I had a quick look, might have to change the raycast from location since we’re no longer using the positions from eye position to the screen/near clip plane and then projecting.
More using the click position on screen as the origin and casting forward.
okie doke, hope this helps if you want an ortho camera.
its probably not the prettiest and more than likely theres other ways of doing it.
so what i had to do was move the raycast3d node, as thats the origin of the cast.
but heres a snippit of the code that i tested with an ortho camera.
this is in the ray_picker_script
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
# so i got the 2d position of mouse on canvas
var mouse_position_2d = get_viewport().get_mouse_position()
# converted that 2d mouse position, into 3d global space, ith a z depth of 0
var mouse_position_3d = project_position (mouse_position_2d, 0)
# move the raycast object to that position
ray_cast_3d.global_position = mouse_position_3d
# then cast from where the raycast3d object is, then along the normal
ray_cast_3d.target_position = project_local_ray_normal(mouse_position_2d)
ray_cast_3d.force_raycast_update()
Wow, that works as intended! I saw that diagram on the godot tutorials page too. The godot docs say project_ray_origin is useful for object intersection or picking, so I did not realize you are supposed to use “project_position(mouse_position_2d, 0)” instead of “project_ray_origin(mouse_position_2d).” Your solution helps a lot, thank you!