Spawn enemies on spawners

Goal is to spawn enemy on a spawner. What do you think of my script and how you will do it ?

private void SpawnEnemy()
   {
       for (int i = 0; i < 32; i++)
       {
           int randomSpawnerIndex = Random.Range(0, _spawnerArray.Length);
           Vector3 spawnLocation = _spawnerArray[randomSpawnerIndex].transform.position;
           Vector3 randomPoint = new Vector3(Random.insideUnitCircle.x, 0f, Random.insideUnitCircle.y) * 5f + spawnLocation;
           if (!Physics.BoxCast(randomPoint, _pfEnemy.transform.localScale, transform.up, out RaycastHit hit, transform.rotation, 20f, _spawnLayerMask))
           {
               GameObject go = Instantiate(_pfEnemy, randomPoint, Quaternion.identity);
               _enemyList.Add(go);
               break;
           }
       }
   }

Does it work? Ship it!!

Euh yeah but i want a review.

If it works the way you intended it to, it’s fine. On first glance, i can’t see any obvious improvements, except maybe turning your magic numbers (the circle radius and BoxCast distance) into variables for if you want to change them later.

1 Like
   new Vector3(Random.insideUnitCircle.x, 0f, Random.insideUnitCircle.y) *

Don’t do this. It will generate two different points in a circle and then take x from one and y from other. Such point isn’t guaranteed to be in a circle anymore. For example first point might be (-1, 0) and second (0, 1), but (-1, 1) isn’t in a circle with radius of 1.

You need to save the result of Random.insideUnitCircle in a temporary variable and then take components from that temporary variable.

3 Likes

Yeah i did it.
Btw i don’t really understand what distance do here.

The box is cast, not just overlapped.

Physics.OverlapBox(): “given this cardboard box static in place in space, tell me EVERYTHING it is hitting.”

Physics.BoxCast(): “given this cardboard box, pushed through space on a vector for a distance of X, what is the first piece of stuff it hits?”

Physics.BoxCastAll(): “same as BoxCast, but once you hit something, keep going the full distance, tell me EVERYTHING the box hits as it passes through space”

** with “everything” meaning “every collider meeting layering conditions”

Yes i did it.

Vector2 randomPointOnACircle = Random.insideUnitCircle;
            float radius = 5f;
            Vector3 randomPoint = new Vector3(randomPointOnACircle.x, 0f, randomPointOnACircle.y) * radius + spawnLocation;

But then (-1, 1) what is it if it isn’t a radius of 1 ?

Ok but since spawner are on the floor i distance of 1f should be enough ?

Is the box starting out already intersecting? Generally when your cast anything you have to NOT be impinging on it and you have to go through the outer facing of the collider.

I am not quite sure what you meant with that question.

If you are not good at visualizing coordinates and aren’t convince that point (-1, 1) is outside the unit circle here is an illustration (for clarity using [-0.9, 0.9] but idea is similar).
8882910--1213758--upload_2023-3-17_9-25-18.png

If you need circle with radius other than 1, you multiply resulting vector by radius just like you already did.

If the question was “whether something bad will happen if point is outside the circle?”, that depends on your intention for the whole spawning process. If you didn’t mean to generate a point in some circle why are using Random.insideUnitCircle ?

If you wanted to ask what kind of shape it will result in - it will be a 2x2 square with nonuniform distribution, and much higher probably in the center than corners. If you actually wanted a square I would recommend calling Random.value or Random.Range twice.

1 Like

Did you draw this yourself or find it on internet

It was just a few minute work in inkscape. Using appropriate snapping settings and being familiar with software helps drawing such stuff quickly. If you were expecting for some fancy graphic drawing software which draws stuff based on entered coordinates I have to disappoint you.

inkspace is very good ı think I use krita and for pixel art aseprite

My spawner is on the ground

I just want to spawn randomly around the spawner.