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. Dismiss Notice

Question Making an enemy no spawn zone for wave-based top down shooter

Discussion in 'Scripting' started by itsdawg43, May 26, 2023.

  1. itsdawg43

    itsdawg43

    Joined:
    Mar 7, 2023
    Posts:
    11
    hey, im making a wave based top down shooter and was trying to figure out a way to make a dead-zone around the player where enemies can't spawn. my current spawn script chooses a random position and then spawns the enemy there after a certain delay, but then the problem arises where sometimes enemies spawn on top of the player, and they then take unfair damage. i was thinking some way to check the distance from the player on start, then re-randomizing the position of the enemy if it's too close. any pointers on this would be appreciated, thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    That would be the magic!

    Vector3.Distance() is your friend

    And of course your dawg
     
  3. itsdawg43

    itsdawg43

    Joined:
    Mar 7, 2023
    Posts:
    11
    got it to work. heres the code for anyone interested:
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if(!finishedSpawn)
    4.         {
    5.             if (Vector3.Distance(transform.position, playerTransform.position) < 15)
    6.             {
    7.                 transform.position = new Vector2(Random.Range(-38, 38), Random.Range(-38, 38));
    8.             }
    9.             else
    10.             {
    11.                 finishedSpawn = true;
    12.                 aiScript.enabled = true;
    13.                 sr.enabled = true;
    14.             }
    15.         }
    16.     }
     
    AngryProgrammer likes this.