Raycast end position being offset

Godot Version

4.2

Question

I have weapons on the vehicle that I want to point at the mouse cursor, however it seems to be offset more and more as player moves away from the start position? I’ve tried changing self.global_position to a self.position but that resulted in even weirder behavior of the weapon bodies, what am I doing wrong?

image

video demo: 7.58 MB file on MEGA

You have to work in global coordinates, try to_global(origin), and using position (don’t use self, it’s not needed and makes you not use checks)

Thanks for the reply! Did you mean something like this?

image

This results in a stuttery rotation, most of it is just not being executed because rayResult == 0 which implies that the ray isn’t hitting anything to begin with and rarely it does hit something (?) which just shifts the rotation to an unrealistic one, are there any debug tools to preview how rays are casted with some lines? That could provide to be useful right now.

You also need to do it for end and after generating end, best to do it in the create call

image

Tried that and still get this stuttering rotation behavior, it was closer to intended before the changes made but that didn’t work as intended either

demo: 3.56 MB file on MEGA

You still need to use global_position below, did you do that?

Forgot about this one, it does seem to hit the world body more frequently now but still isnt nowhere close to my mouse position

Does it work if you just do: var angle = Vector2(...).angle_to_point(...) instead?

ive noticed something weird with this setup - it still hits something despite my mouse being pointed to the sky (which would mean no ray collision)

demo: mega.nz/file/MIcEhKAB#tEX0-ryfPvhrMIdjG7MoIhkqLtZE8XVr83pCV33lxRA

(and nope it still doesnt work as intended)

solved with following implementation:

image

func _physics_process(delta):
image

last two lines in latter screenshot are adjustments to rotate the gun node in the proper direction