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()

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

# Configure the RayCast



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() :
:bust_in_silhouette: Reply From: DrPlamsa


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)

Try this Way