The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

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)
+3 votes
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 (20 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 Frequently asked questions and 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 [email protected] with your username.