So ive tried to cast a ray from my enemy to my player and that works fine, the problem is when I try to draw that line something goes wrong. I have not been able to figure this out so I'm asking you.

Code: https://justpaste.it/5a892 (sorry for this weird website, pastern didn't work)

Screenshot of the bug: https://ibb.co/y0fSvvM

This is my first time posting anything here so sorry for any mistakes and also thanks to any help that might come

(also im in 2D and using 3.2.2)

Welcome to the forums @Ierdyr!

The issue could be that you are using hitPos - position to get the offset for the line, but the raycast position returned is in global-space/world-space while position is the node's offset relative to its parent.

I would try the following in the _draw function:

func _draw():
	draw_line(Vector2(), (hitPos - global_position).rotated(-rotation), drawColor)

Thank you for your reply @TwistedTwigleg it was probably one of the most polite and nice answers i've got in a long time. Your suggestion unfortunately did not work and the problem remains the same. But I really appreciate that you took your time to try and help me. Thank you

Thinking about it, I think the _draw function takes coordinates relative to the object. In that case, you may need to transform the hitPos position by the global transform of the node so its position is in local space. Thankfully, there is a helpful function called to_local that is great for this purpose:

func _draw():
	draw_line(Vector2(), to_local(hitPos), drawColor)

I totally forgot about the to_local function :sweat_smile: Using to_local should also handle node rotation, if I recall correctly.

Another thing to check that could be causing the issue is while raycasting to use global positions rather than local ones (global_position rather than position). I do not think that is causing the issue, but it might be something to check if the raycast does not return the expected results.

Heck yea it's working, thank you so much! You're the best!!

moderator note: A curse word was replaced here.

2 years later