0 votes

Hello!

I have an array of Vectors2. The size of the array goes from 0 (empty) to 3.

I want to check If any 2 points inside it exceed a given maxDistance and then turn a variable false (ex.: allowShoot = False).

I know I have the distance_to method but I don’t know how to construct the loop to make that (if a loop it’s what I need I mean).

Thank you very much!

in Engine by (58 points)

Distance towards what? You mean, if any point is further than any other point by a given distance? Or is it distance towards the same point?

2 Answers

0 votes
Best answer

I don't know if there's an engine method to check distances in an array of vectors. But here's a loop to check each frame if any of the distances within an array exceeds a value:

func _process(delta):
var vector_array = []
var maxdistance = 100.0
for idx in range(0, vector_array.size()-2):
    var p0 = vector_array[idx]
    var p1 = vector_array[idx+1]
    if p0.distance_to(p1) > maxdistance:
        allowShoot = false
        break
pass

EDIT: previous loop range could crash the game, fixed it.

by (190 points)
selected by

This worked perfectly thank you!!!

And sorry to the other people commenting for not explaining myself well enough!

0 votes

I understood you are working in 2D. I have only worked in 3D, but it should be almost the same:

var PlayerPos = self.get_global_transform().origin
var PointsArray = [Vector2(0, 0), Vector2(5, 5)]

for Point in PointsArray:
    var DistanceToPoint = PlayerPos.distance_to(Point)

    if DistanceToPoint > 10:
        allowShoot = false

ORIGIN is a property of TRANSFORM, and is a Vector2 in 2D and a Vector3 in 3D. Since you already have a position in Vector2 format, you just need your player position in Vector2.

by (120 points)
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.