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 fleap3 · Jul 18, 2016 at 04:55 AM · c#terrainnavmeshspawningspawning-enemies

NavMesh Sample Position Help

I've been trying to spawn enemies withing a radius around my player. I'm able to do this but sometimes the enemies will spawn on a part of my terrain that doesn't have a Navmesh on it, the enemies then cause errors because they can't get to the player. I've been trying to get my enemies to only spawn on parts of the terrain that will allow them to go to the player but have found an inefficient solution. The way my code works right now it will try 800-4000 random locations until it can spawn enough enemies, this causes lag and is obviously not a great way of doing it. I'm probably misusing NavMesh.SamplePosition so any help would be appreciated, everything I've found so far about it is very vague.

Code in C#

 using UnityEngine;
 using System.Collections;
 
 public class EnemySpawner : MonoBehaviour
 {
 
     public GameObject player, zombiePrefab, zombie, parent;
     public float radius = 10f;
     public int zombieNamePostfix = 0;
     public int maxZombies, minZombies;
     Vector3 SpawnPos = new Vector3(0, 0, 0);
 
     void SpawnEnemy()
     {
         bool areaIsOnMap = false;
         while (!areaIsOnMap)
         {
             float playerX = player.transform.position.x;
             float playerZ = player.transform.position.z;
             SpawnPos.x = Random.Range(playerX - radius, playerX + radius);
             SpawnPos.z = Random.Range(playerZ - radius, playerZ + radius);
             SpawnPos.y = Terrain.activeTerrain.SampleHeight(SpawnPos) + 2.0f;
             NavMeshHit hit;
             areaIsOnMap = (NavMesh.SamplePosition(SpawnPos, out hit, 1.0f, NavMesh.AllAreas));
             print(areaIsOnMap);
         }
         
         GameObject zombie = Instantiate(zombiePrefab, SpawnPos, Quaternion.identity) as GameObject;
         zombie.transform.parent = parent.transform;
         zombie.name += zombieNamePostfix;
         print(SpawnPos.ToString() + areaIsOnMap.ToString() + zombie.name);
         zombieNamePostfix++;
         zombie.GetComponent<UnityStandardAssets.Characters.ThirdPerson.AICharacterControl>().target = player.transform;
     }
 
     void Update()
     {
         if (parent.transform.childCount < minZombies)
         {
             int zombiesToSpawn = Random.Range(minZombies - parent.transform.childCount, maxZombies - parent.transform.childCount);
             int counter = 0;
             while (counter <= zombiesToSpawn)
             {
                 counter++;
                 SpawnEnemy();
             }
         }
     }
 }

Thanks in advance, fleap.

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
1
Best Answer

Answer by CodeElemental · Jul 18, 2016 at 11:45 AM

From what i can see is that the initial Y spawn point is 2 units above the player , and the search radius is 1 unit. Can you try making the radius bigger (for example 3 units)?

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 fleap3 · Jul 18, 2016 at 01:30 PM 0
Share

Oh wow thank you that fixed it, I wasn't sure what that radius meant from the documentation. So would I just be better off setting that to the same as my radius variable? Also if you want to copy that into an answer I can accept it as the best answer.

avatar image CodeElemental fleap3 · Jul 18, 2016 at 01:48 PM 0
Share

What i would recommend is you start the search at terrain level (so that you get coordinates - SpawnPoint) using 1 unit of radius. Later when you do the actual spawn, you can offset it another 2 units vertically.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Spawning prefabs dependant upon Health UI? 2 Answers

Trying to make a spawning enemy code 1 Answer

Spawn Objects on Walkable Surfaces 1 Answer


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