• 3D
  • Clicking on a gridmap from an Orthogonal camera

I'm working on a script that returns the position on a gridmap in 3d from a mouse click. Here's my code:

    extends Camera
    
    var target: Spatial
    
    onready var ray := $RayCast
    
    func _input(event: InputEvent) -> void:
    	if event is InputEventMouseButton or event is InputEventScreenTouch:
    		if event.pressed:
    			_select_target(event.position)
    
    func _select_target(position: Vector2) -> void:
    	var to = project_local_ray_normal(position) * 10000
    	ray.cast_to = to
    	ray.force_raycast_update()
    	print("click")
    	if ray.is_colliding():
    		var point = ray.get_collision_point()
    		print(point)

So, this code works flawlessly except for one thing: It does not work with an orthogonal camera, which is what I need it to do. From what I can dig up in the docs I don't know why or how to fix it since project_local_ray_normal should work in either camera mode(i think).

Anyone have insight?

Ok, the docs page was some help, but honestly the example for implementing 3d raycasting from screen could be a little more complete than providing the necessary variables. I finally got it to work though!

extends Camera

var target: Spatial

onready var ray := $RayCast

func _input(event: InputEvent) -> void:
	if event is InputEventMouseButton or event is InputEventScreenTouch:
		if event.pressed:
			_select_target(event.position)

func _select_target(click_position: Vector2) -> void:
	var from = project_ray_origin(click_position)
	var to = from + project_ray_normal(click_position)*1000
	var space_state = get_world().direct_space_state
	ray.force_raycast_update()
	print("click")
	if ray.is_colliding():
		var iray = space_state.intersect_ray(from, to)
		var point = iray["position"]
		print(point)

Yeah me too. Thanks for pointing me in the right direction.