• Godot Help
  • How do I get the global position of the end point of a moving RayCast3D?

Hi,

I have a RayCast3D attached to my characters first person head. How do I get the end point of the RayCast3D when it doesn't collide with anything, in global coordinates? The RayCast3D moves with the head. It is used to check if the player hit the target with his gun.

Thank you!

  • Lethn Hey, we solved it by putting an empty Node3D (we're using 4) at the end of the ray and using its global_position. This way, we don't have to play around with collision layers and stuff like that.

    Thanks for your idea though!

Lethn That method doesn't give sensible data when the Ray isn't colliding with anything. My use case is that if the player looks into the sky, the bullets should still go to where the end of the ray is. I need to find the end point of the ray when it is NOT colliding with anything. get_collision_point() returns (0, 0, 0) if that is the case.

    gowner Ah, in that case the common trick is to create an invisible wall for your ray to shoot at, you're still physically colliding with something but as far as the player is concerned they're shooting off into the distance and sorry, didn't properly read your OP and thought you were just asking about getting raycast data.

      Lethn Hey, we solved it by putting an empty Node3D (we're using 4) at the end of the ray and using its global_position. This way, we don't have to play around with collision layers and stuff like that.

      Thanks for your idea though!

        gowner That's clever! I hadn't thought of that.

        10 months later

        How about raycast.target_position - raycast.global_position

          4 days later

          Drachenbauer

          I think you meant raycast.target_position + raycast.global_position which makes sense at first because I was trying it but it was not working. The reason it does not work is that the raycast.target_position does not return the global position of the raycast target positions; it returns the local target position of the the scene that the raycast is in. So let’s say the player was to rotate, then the raycast would rotate with the player but the target position of the raycast would always stay the same since it is relative to the player scene. So when adding the raycast global position to the raycast target position we would be getting a vector that is pointing towards the original target position (in this case the original target position is where player is looking) and if the player was to rotate in the scene that we are working with we would still get the same target position as if the player never rotated. I hope this explains it well. Side note, if the object that we are working with does not rotate in the scene where you are getting global position then you would get the correct position that the ray is pointing at.

          • xyz replied to this.

            Narada

            to_global(target_position)

              xyz

              Oh I didn’t know that function existed. I’m still 1 week into GoDot. Thank you!

              • xyz replied to this.

                Narada Just note that it is a method of Node3D (and Node2D). It transforms given coordinate from node's local space to global space. There's also its counterpart to_local() which does the opposite.

                If we can get the global position of the raycast target position, then we don’t have to add anything since it’s already the global position. The whole point of the raycast.target_position + raycast.global_position was to get the global target position of the ray cast. So Darchenbauer was maybe trying to get the direction of the raycast by subtracting.

                • xyz replied to this.

                  Narada I don't know what they meant but the original question was about global target position. So was the accepted answer. Doing it with to_global() is the simplest, most straightforward way. And it avoids managing additional nodes.