• 2D
  • Make distance_to & draw_line use same units to draw range indicator

Hello there. I am currently building a tower defense game and I am currently a little stuck at drawing a range indicator around the attacker. The plan is to use the following function to draw a circle around the attacker at its max. range of operation. The range is stored in a variable as an integer and also used to calculate the distance between attacker and target:

eg:

var range = 500
if attacker.global_position.distance_to(target.global_position) < range:
        shoot()

Draw function

func draw_circle_arc(center, radius, angle_from, angle_to, color):
	var nb_points = 2048
	var points_arc = PoolVector2Array()

	for i in range(nb_points+1):
		var angle_point = angle_from + i*(angle_to-angle_from)/nb_points - 90
		var point = center + Vector2( cos(deg2rad(angle_point)), sin(deg2rad(angle_point)) ) * radius
		points_arc.push_back( point )

	for indexPoint in range(nb_points):
		draw_line(points_arc[indexPoint], points_arc[indexPoint+1], color, 2)

In my understanding the range should be the radius of the circle but however the circle is way to big when given the range as the radius.

Imgur

This is what I want to achieve. The circle is located right where the attacker starts shooting the targets. But in this case I had to divide the range by 5 to get this result. Otherwise it would be way to big. So my question is: How can I manage to use the same distance/range variable for both checking the shooting range and drawing the range indicator? Aren't both using pixel measurements?

Thanks in advance!

This discussion was caught in the moderation queue since you have not confirmed your account yet.
Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.
If you need any help, please let us know! You can find ways to contact forum staff on the Contact page. Thanks! :smile:

Is the CanvasItem you're drawing the line on being scaled, or inheriting scale from a parent?

@duane said: Is the CanvasItem you're drawing the line on being scaled, or inheriting scale from a parent?

I didn't think that I transformed something but I checked and actually the scale value was set to 500 which completely explains why I have to divide the range by 5 in my case to make it fit. I changed the scaling and adjusted everything accordingly. Now it works like a charm! Thanks! I just wonder when the scaling happend, something I will probably never find out :#