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 alpenid2004 · Mar 02, 2020 at 08:23 PM · enemy spawndistance check

Enemies distance from player

Hi everyone, thanks for giving me attention. So basically I got a simple script to spawn enemies at a certain range around the player, but I don't want them to spawn too close to the player so I tried something like this.

 private void Start()
 {
     InvokeRepeating(nameof(createRandomEnemy), 0.0f, cooldown);
 }
 
 void createRandomEnemy()
 {
     float xRand = Random.Range(0, xLimit);
     float zRand = Random.Range(0, zLimit);
     Vector3 spawningPos = new Vector3(xRand, 2, -zRand);
 
     if (Vector3.Distance(player.position, spawningPos) < minDistance)
     {
         Debug.Log("Too close");
         return;
     }
     Instantiate(enemy, spawningPos, Quaternion.identity);
     }

what I want this to do is check if the location is too close to the player and if that's the case then just choose another position but when I use return the function doesn't resets (I know that it just breaks the function but IDK what else to use) and it just waits for the cooldown and invokes again, basically skipping the enemy. if any of you got any solutions to this I'd be really happy to see it.  

any kind of help is greatly appreciated.

Thanks a lot!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by unity_ek98vnTRplGj8Q · Mar 02, 2020 at 08:55 PM

The easy way is to use a do-while loop that keeps giving you positions until it finds one that works. I've added a count variable so that it does't freeze up if it cant find a good spawn location after a few attempts

 void createRandomEnemy () {
 
     Vector3 spawningPos;
     int debugCount = 0;
     int maxTries = 50;
 
     do{
         float xRand = Random.Range (0, xLimit);
         float zRand = Random.Range (0, zLimit);
         spawningPos = new Vector3 (xRand, 2, -zRand);
         debugCount ++;
     }
     while ((Vector3.Distance (player.position, spawningPos) < minDistance) && debugCount < maxTries);
 
     if(count >= maxTries){
         Debug.Log("Could not find a good spawn position after " + debugCount + " attempts.");
         return;
     }
 
     Instantiate (enemy, spawningPos, Quaternion.identity);
 }
Comment
Add comment · Show 2 · 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 unity_ek98vnTRplGj8Q · Mar 02, 2020 at 08:57 PM 0
Share

There are more efficient ways to do this but honestly this should be fine for your purposes

avatar image alpenid2004 · Mar 03, 2020 at 07:41 AM 0
Share

Thanks a lot for taking your time and helping me out. This works perfectly fine.

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

122 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

Related Questions

How To Deactivate Enemy Shooting 1 Answer

wave spawner with different enemies in the waves 2 Answers

I want to setup a raycast that is going forward from my character that allows me to left click on a cube with the tag "npc" and open up a gui window (in C#) 2 Answers

How do I check the distance between multiples objects in an array from the player? 4 Answers

Increase number of enemies spawned per level 0 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