0

I want to do the controls like in Dota. There, when you click on any terrain, a point appears where the character needs to go. How to make this system? I am trying to do this with rays but cannot find the end position:

var space_state = get_world().direct_space_state
if Input.is_action_pressed("LefftClick"):
        _to_ray_cast = translation
        _to_ray_cast.y -= 15
        ray_cast = space_state.intersect_ray(
            translation, _to_ray_cast)
        print(ray_cast)

How to do it ? The screen size may change

Example. https://i.ibb.co/SdMJ5vW/2021-05-19-21-23.png I hover over the red dot and click. I need the exact cardinals in the world where I clicked. There's a wall next to these coordinates

1 Answers1

0

If you want to make a raycast from the camera to where the user is pointing, use these:

var ray_length = 100 # some large number
var mouse_pos = get_viewport().get_mouse_position()
var from = camera.project_ray_origin(mouse_pos)
var to = camera.project_ray_normal(mouse_pos).normalized() * ray_length

Where camera is the current Camera.

Then from and to are the parameters for intersect_ray.

Theraot
  • 31,890
  • 5
  • 57
  • 86