• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Galatia410 · Dec 30, 2017 at 02:19 AM · scripting problempositionscripting beginnerscriptingbasicsdistance

randomly Instantiate items

I'm trying to Instantiate multiple enemy spawn points at random locations when my level loads, making sure they don't overlap within a certain radius, of each other, main character, and a boss area. everything is almost working great but from what i can tell nothing is being spawned to the right or down from any of the other already placed objects. its also taking 30sec to 1 min to calculate the 20 spawn points... If anyone can help with this issue it would be greatly appreciated. I'm also attacking a screenshot of whats happening. the white dots are placeholders for the enemy spawnpoints.

Varriable to check if valid position:

     static public bool CheckIfInSquare(Vector3 NewObjectPosition, Vector3 OldObjectsPosition, float RadiusBetweenObjects){
         if (NewObjectPosition.x < OldObjectsPosition.x - RadiusBetweenObjects || NewObjectPosition.x > OldObjectsPosition.x + RadiusBetweenObjects
             && NewObjectPosition.z < OldObjectsPosition.z - RadiusBetweenObjects || NewObjectPosition.z > OldObjectsPosition.z + RadiusBetweenObjects){
             return true;
         } else{
             return false;
         }
     }


Level Script

 // Public
 public GameObject BossArea01;
 public GameObject SpawnPointMarker01;
 // Private
 private Vector3 BossArea01Position;
 private GameObject Spawnpoint01;
 private GameObject Spawnpoint02;
 private GameObject Spawnpoint03;
 private GameObject Spawnpoint04;
 private GameObject Spawnpoint05;
 private GameObject Spawnpoint06;
 private GameObject Spawnpoint07;
 private GameObject Spawnpoint08;
 private GameObject Spawnpoint09;
 private GameObject Spawnpoint10;
 private GameObject Spawnpoint11;
 private GameObject Spawnpoint12;
 private GameObject Spawnpoint13;
 private GameObject Spawnpoint14;
 private GameObject Spawnpoint15;
 private GameObject Spawnpoint16;
 private GameObject Spawnpoint17;
 private GameObject Spawnpoint18;
 private GameObject Spawnpoint19;
 private GameObject Spawnpoint20;
 private int SummonSpawnPointInt = 1;
 // ********************************   Start   *****************************************
 void Start () {
     // Randomly Select Boss Area Position
     int rand = Random.Range (1, 5);
     if (rand == 1){
         BossArea01Position = new Vector3 (150, .02f, 150);
     } else if (rand == 2){
         BossArea01Position = new Vector3 (-150, .02f, -150);
     } else if (rand == 3){
         BossArea01Position = new Vector3 (150, .02f, -150);
     } else if (rand == 4){
         BossArea01Position = new Vector3 (-150, .02f, 150);
     }
     // Summon Boss Area
     Instantiate (BossArea01, BossArea01Position, Quaternion.identity);
 }
 // *******************************   Update   *****************************************
 void Update () {
     // Randomly Summon SpawnPointMarker
     if (SummonSpawnPointInt <= 20){
         Vector3 position = new Vector3 (Random.Range (-175, 175), 1, Random.Range (-175, 175));
         if (Galatia420.CheckIfInSquare (position, Character01Movement.MainCharacterPosition, 20)){//20
             if (Galatia420.CheckIfInSquare (position, BossArea01Position, 35)){//35
                 if (Spawnpoint01 == null || Galatia420.CheckIfInSquare (position, Spawnpoint01.transform.position, 10))//20
                 if (Spawnpoint02 == null || Galatia420.CheckIfInSquare (position, Spawnpoint02.transform.position, 10))
                 if (Spawnpoint03 == null || Galatia420.CheckIfInSquare (position, Spawnpoint03.transform.position, 10))
                 if (Spawnpoint04 == null || Galatia420.CheckIfInSquare (position, Spawnpoint04.transform.position, 10))
                 if (Spawnpoint05 == null || Galatia420.CheckIfInSquare (position, Spawnpoint05.transform.position, 10))
                 if (Spawnpoint06 == null || Galatia420.CheckIfInSquare (position, Spawnpoint06.transform.position, 10))
                 if (Spawnpoint07 == null || Galatia420.CheckIfInSquare (position, Spawnpoint07.transform.position, 10))
                 if (Spawnpoint08 == null || Galatia420.CheckIfInSquare (position, Spawnpoint08.transform.position, 10))
                 if (Spawnpoint09 == null || Galatia420.CheckIfInSquare (position, Spawnpoint09.transform.position, 10))
                 if (Spawnpoint10 == null || Galatia420.CheckIfInSquare (position, Spawnpoint10.transform.position, 10))
                 if (Spawnpoint11 == null || Galatia420.CheckIfInSquare (position, Spawnpoint11.transform.position, 10))
                 if (Spawnpoint12 == null || Galatia420.CheckIfInSquare (position, Spawnpoint12.transform.position, 10))
                 if (Spawnpoint13 == null || Galatia420.CheckIfInSquare (position, Spawnpoint13.transform.position, 10))
                 if (Spawnpoint14 == null || Galatia420.CheckIfInSquare (position, Spawnpoint14.transform.position, 10))
                 if (Spawnpoint15 == null || Galatia420.CheckIfInSquare (position, Spawnpoint15.transform.position, 10))
                 if (Spawnpoint16 == null || Galatia420.CheckIfInSquare (position, Spawnpoint16.transform.position, 10))
                 if (Spawnpoint17 == null || Galatia420.CheckIfInSquare (position, Spawnpoint17.transform.position, 10))
                 if (Spawnpoint18 == null || Galatia420.CheckIfInSquare (position, Spawnpoint18.transform.position, 10))
                 if (Spawnpoint19 == null || Galatia420.CheckIfInSquare (position, Spawnpoint19.transform.position, 10))
                 if (Spawnpoint20 == null || Galatia420.CheckIfInSquare (position, Spawnpoint20.transform.position, 10))
                     // Summon
                     switch (SummonSpawnPointInt){
                     case 1:
                         Spawnpoint01 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 2:
                         Spawnpoint02 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 3:
                         Spawnpoint03 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 4:
                         Spawnpoint04 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 5:
                         Spawnpoint05 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 6:
                         Spawnpoint06 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 7:
                         Spawnpoint07 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 8:
                         Spawnpoint08 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 9:
                         Spawnpoint09 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 10:
                         Spawnpoint10 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 11:
                         Spawnpoint11 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 12:
                         Spawnpoint12 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 13:
                         Spawnpoint13 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 14:
                         Spawnpoint14 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 15:
                         Spawnpoint15 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                 
                         break;
                     case 16:
                         Spawnpoint16 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 17:
                         Spawnpoint17 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 18:
                         Spawnpoint18 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 19:
                         Spawnpoint19 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         break;
                     case 20:
                         Spawnpoint20 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
                         SummonSpawnPointInt += 1;
                         Debug.Log ("Level01: Done Summoning Spawnpoints");
                         break;
                     }
             }
         }
     }
 }

p.s. if anyone has a good suggestion on how to shorten this... lol i'm up for suggestions ;) .

alt text

capture.png (375.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

187 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The type or namespace function could not be found 1 Answer

Words in my script don't turn blue, why not? (absolute script noob) 3 Answers

GameObject.Find : why not working? 3 Answers

How to add information to an instantiated object 0 Answers

Merge Points collection system via a separate script 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges