0 votes

I have been following this tutorial on ray casting
http://kidscancode.org/blog/2018/03/godot3_visibility_raycasts/

It is a very well written tutorial and I can understand it quite nicely up to this line

draw_line(Vector2(), (hit_pos - position).rotated(-rotation), laser_color)

I looked up help document , the first parameter is where the line starts to draw, in this case it is a location of a turret. Which I would assume the line should be something like

 draw_line(position, (hit_pos - position).rotated(-rotation), laser_color)

script is attached to turret hence the position.
But when I tested , the line is drawn from top of the screen to the target. And if I changed back to vector2() then, it the line would be drawn correctly from turret to target

I am a bit confused on why it is. Am I missing something here ?

in Engine by (168 points)
edited by

1 Answer

0 votes
Best answer

When you draw_line the positions you pass is relative to the node you draw on not absolute. So Vector2() passed in becomes position and passing position as the first arguments means the line will start at position + position.

Because of this, if the turret could move, you will likely see the start point of the line moving around.

by (4,081 points)
selected by

Thank you. It actually make a lot of sense, I just wish the help doc in Godot could be more clear about this

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 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 webmaster@godotengine.org with your username.