0 votes

I wantt to use the ray-casting to get the cursor position on a plane to make a ball roll to the position. But I found that ray-casting could not identity the correct position. So I added an ImmediateGeometry node to draw the rays in the world to check what happened. Here is what I found:image

The origin of camera is infront of my view port and the rays are obviously not correct for usage because their origins are even infront of my cursor! I don't know why it happens, and here is my code:

    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.is_pressed():
    if Input.get_mouse_mode()==Input.MOUSE_MODE_VISIBLE:
        var from=project_ray_origin(event.position)
        var to=from+project_ray_normal(event.position)
        G.begin(PrimitiveMesh.PRIMITIVE_LINES)
        G.add_vertex(from)
        G.add_vertex(to)
        G.end()

It's very strange, I also tried to move and create some other rays from another origin. In short, whereever I am I can always rotate my camera and see the origin of my camera. It is like you can rotate your eyes 180 degrees to see your brain. And because of this, what is shown in the viewport is not coincide the real camera position.

Godot version 3.2.3
in Engine by (16 points)

1 Answer

+1 vote

i cant see the picture because this forum does not let you, but if you want the ray to start from the camera, use $Camera.global_translation as the starting point, and $Camera.project_position(screen_coordinate, distance) as final point

by (1,514 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.