A more circular maxDistance

I know you said this will change soon, but I had to create a more circular set of allowable grids using this.

int testDistance = (Mathf.Abs(x) * Mathf.Abs(x)) + (Mathf.Abs(z) * Mathf.Abs(z));

if (testDistance > (maxShootDistance * maxShootDistance))
{
continue;
}

5 Likes

Nice.

You don’t have to Mathf.Abs() those values because they will be positive once you multiplied them, so

int testDistance = (x * x) + (z * z);

will be ever so slightly more performant

4 Likes

Very good point, cheers :slight_smile:

Yup that’s a great simple way to do that. The final version later on in the course asks the Pathfinding system to get the path length

3 Likes

Do you see those single grid position “spikes” you get at 0,7 and 7,0? Those may not be to your taste. If you want to have a subjectively nicer-looking circle, you can add 0.5f to your maxShootDistance before squaring it.

https://www.redblobgames.com/grids/circle-drawing/#aesthetics

5 Likes

Oh that’s an interesting tip! Personally I still prefer the main method since I like the strategy of moving further if going straight but that’s a great tip!

I did something similar:

     //making a circular shooting range:
                double radiusDistance = Math.Sqrt((x*x) + (z*z)); 
                int testDistance = Convert.ToInt32(radiusDistance);
                if (testDistance > maxShootDistance)``

Privacy & Terms