Raycast 3D from mouse position. Ortogonal

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By David Marruecos

Hi, i’m having a hard time trying to raycast, it works in the sense that it detects things, but i don’t manage to get the ray moving to the mouse position casting the ray “Aka : Ray from mouse perpendicular to screen plane”

I have already tried with

But it don’t work

The setting,
Ortogonal camera
Rotated -30 degre on X
Translated to (0,5,5)

Script that creates the ray

func _ready():
# Create the RayCast
ray = RayCast.new()
ray.set_name("RayCast_cells")

# Atach the RayCast to the camera
camera = get_tree().get_root().find_node("Camera", true, false)
camera.add_child(ray)

# Configure the RayCast
ray.clear_exceptions()

ray.set_collision_mask(COLLISIONMASK)
ray.set_enabled(true)

set_physics_process(true)

And now the _physics_process

func _physics_process(delta):
var mouse = get_viewport().get_mouse_position()

var from = camera.project_ray_origin(mouse)
var to = (from + camera.project_ray_normal(mouse))


ray.cast_to = to
ray.translation = from

if ray.is_colliding() :
	print("found")
:bust_in_silhouette: Reply From: DrPlamsa

Hello:

In the API, cast_to is said to be defined relative to the ray’s position, not the global position. Try setting to to be just camera.project_ray_normal(mouse))

:bust_in_silhouette: Reply From: मयंकेला
func _input(event):
	if event is InputEventMouseButton :
		var camera = get_node("-Camera")
		var from = camera.project_ray_origin(event.position)
		var to = from + camera.project_ray_normal(event.position) * 100
		var cursorPos = Plane(Vector3.UP, transform.origin.y).intersects_ray(from, to)
		print(cursorPos)

Try this Way