2D Distance between Objects?

Godot Version

4.2.2

Question

I would just like to know if there is a simple, easy-to-use function that will allow me to calculate to distance between two objects an 2D.

I need to know how far apart it is between the “hero” and the “baddie” to see if they are in attack range. I had code that does work sometimes, but, now I am trying to force the issue in a last ditch attempt to get it working right.

As always any help is greatly appreciated.
Regards.

Sure just use distance_to:

var distance = global_position.distance_to(target.global_position)
2 Likes

Yep! That worked. Thankyou :sunglasses:.

I won’t even tell you what I was trying before that - that was so much easier.

Unfortunately the “baddie” characters still won’t take energy off me on every attack for reasons I can’t fathom, I tried turning of the “invincibility” timer and every other thing I could think off. Which to be frank isn’t that much.

Guess I have to leave it for now! And have it come back to bite me on the ar$e in the future.

Thanks for the help, knowing the distance made everything so much easier.

1 Like

Actually with a rejigging of code and using that distance function you gave it might be working now. I’ve tested it out a couple of times and it did work but I need to do more testing - it hard the get the conditions just right to get the error/ or not to occur but it looks promising!

Thanks again.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.