Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by AshwinTheGammer · Apr 04, 2020 at 04:40 PM · unity 5ailistoverlappingrandomspawning

Spawning enemy at random spawn points without Overlapping!

Hi Unity Answers,

I want to spawn Enemies at random spawn points without overlapping. So, I'm checking that if the spawn point is used then don't use that spawn point, if it's not then continue... I've an empty gameObject which contains all spawnPoints for enemy. I've assigned the script to that empty gameobejct.

I tried to use Transform as a List for Spawn points but unfortunately, it didn't work. If you can convert this from Vector3 to Transform then, it'll be good. I tried to convert this in Transform but I got so many error. I did try, but, it didn't work as expected.

You can check that script here: Transform Error Script Errors in that script is here: Image Transform error

Now, The script which I'm working on is as follow:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class EnemySpawns : MonoBehaviour {
 
     public GameObject enemy; //my enemy
     public float spawnTime = 3f; //spawn after 3 sec
     public List<Vector3> spawnPoints = new List<Vector3> (); //Vector3 positions of spawnpoints.
     public float distance;   //distance from the spawnPoints
 
 
 
     void Start ()
     {
         //spawnRandom ();
         InvokeRepeating ("spawnRandom", spawnTime, spawnTime); //Call this fucntion after 3sec.
     }
 
     public Vector3 spawnRandom(){
         
         Vector3 newSpawnPoint;  //new spawn point 
         Vector3 random = UnityEngine.Random.insideUnitSphere * distance;
 
         newSpawnPoint = new Vector3 (random.x, 0, random.z);
 
         newSpawnPoint += transform.position;
 
         if (!spawnPoints.Contains (newSpawnPoint)) {
             return newSpawnPoint;
             spawnPoints.Add (newSpawnPoint);
             int spawnIndex = Random.Range (0, spawnPoints.Count);
             Instantiate (enemy, spawnPoints [spawnIndex].normalized, Quaternion.identity);  //Spawn randomly
         } else {
             return Vector3.zero;
 Deug.Log("Enemy is overlapping!");
 
         }
     }
 
 }
 

The problem is: Enemy is not spawning at all! Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by krispastas · Apr 06, 2020 at 01:44 PM

Just check if the enemy is colliding with another enemy.

Comment
Add comment · Share
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
avatar image
0

Answer by Sawyer1424 · Apr 08, 2020 at 03:57 PM

I would have the empty game object go to a random area with a Navmesh that is twice as large as your largest enemy, have it always find a new valid area and go to it, give it a lot of speed and have it instantiate an enemy every three seconds. Give it a rigidbody along with everything else, but give it a lot smaller weight so it can't push your enemies, make these on a separate collision layer so your player can't interact with it.

code for the random movement

   using UnityEngine.AI;
  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  public class EnemySpawns : MonoBehaviour {
 
 public GameObject enemy;
 public float distance = 10; 
 public float spawnTime = 3;
 void Start ()
      {
          //spawnenemy
          InvokeRepeating ("spawn", spawnTime, spawnTime); //Call this fucntion after 3sec.
      }
     void FixedUpdate ()
 {
         Vector3 randomDirection = Random.insideUnitSphere * distance;
                  randomDirection += transform.position;
      NavMeshHit hit;
      NavMesh.SamplePosition(randomDirection, out hit,distance, 1);
      Vector3 fleePosition = hit.position;
      agent.ResetPath();
                 agent.destination = fleePosition;
 }
 
 public void Spawn ()
 {
   Instantiate (enemy, transform.position);
 }
 
 }
  

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

289 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 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

A node in a childnode? 1 Answer

problem in minimax algorithm 1 Answer

Unity random card select system with persentage 1 Answer

UI Test overlapping trouble (in Build) 1 Answer

Navmeshagent wont go after player after stopping! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges