0 votes

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
http://docs.godotengine.org/en/3.0/tutorials/physics/ray-casting.html
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 physicsprocess

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")
in Engine by (12 points)

2 Answers

0 votes

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

by (56 points)
+1 vote
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

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.