Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Feedback Spawn enemies on spawners

Discussion in 'Scripting' started by Pytchoun, Mar 13, 2023.

  1. Pytchoun

    Pytchoun

    Joined:
    Apr 12, 2015
    Posts:
    198
    Goal is to spawn enemy on a spawner. What do you think of my script and how you will do it ?

    Code (CSharp):
    1. private void SpawnEnemy()
    2.    {
    3.        for (int i = 0; i < 32; i++)
    4.        {
    5.            int randomSpawnerIndex = Random.Range(0, _spawnerArray.Length);
    6.            Vector3 spawnLocation = _spawnerArray[randomSpawnerIndex].transform.position;
    7.            Vector3 randomPoint = new Vector3(Random.insideUnitCircle.x, 0f, Random.insideUnitCircle.y) * 5f + spawnLocation;
    8.            if (!Physics.BoxCast(randomPoint, _pfEnemy.transform.localScale, transform.up, out RaycastHit hit, transform.rotation, 20f, _spawnLayerMask))
    9.            {
    10.                GameObject go = Instantiate(_pfEnemy, randomPoint, Quaternion.identity);
    11.                _enemyList.Add(go);
    12.                break;
    13.            }
    14.        }
    15.    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    32,333
    Does it work? Ship it!!
     
  3. Pytchoun

    Pytchoun

    Joined:
    Apr 12, 2015
    Posts:
    198
    Euh yeah but i want a review.
     
  4. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    49
    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.
     
    spiney199 likes this.
  5. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    419
    Code (CSharp):
    1.    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.
     
    samana1407, tomfulghum and D12294 like this.
  6. Pytchoun

    Pytchoun

    Joined:
    Apr 12, 2015
    Posts:
    198
    Yeah i did it.
    Btw i don't really understand what distance do here.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    32,333
    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"
     
  8. Pytchoun

    Pytchoun

    Joined:
    Apr 12, 2015
    Posts:
    198
    Yes i did it.

    Code (CSharp):
    1. Vector2 randomPointOnACircle = Random.insideUnitCircle;
    2.             float radius = 5f;
    3.             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 ?
     
  9. Pytchoun

    Pytchoun

    Joined:
    Apr 12, 2015
    Posts:
    198
    Ok but since spawner are on the floor i distance of 1f should be enough ?
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    32,333
    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.
     
  11. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    419
    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).
    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.
     
    samana1407 likes this.
  12. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    260
    Did you draw this yourself or find it on internet
     
  13. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    419
    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.
     
  14. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    260
    inkspace is very good ı think I use krita and for pixel art aseprite
     
  15. Pytchoun

    Pytchoun

    Joined:
    Apr 12, 2015
    Posts:
    198
    My spawner is on the ground
     
  16. Pytchoun

    Pytchoun

    Joined:
    Apr 12, 2015
    Posts:
    198
    I just want to spawn randomly around the spawner.