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 Hamilcar-games · Jul 02, 2017 at 07:30 PM · instantiateainavmeshnavmeshagentenemy ai

how to instantiate prefabs around the player but when there is an object in that position it dont instantiate

im making a police chasing game and have a player that is chased by police i want that when more time the player is alive the more police cars will be in the scene i have set up a navmesh and a police_ai.cs script that makes the cars follow the player correctly and i have set up the the object in the map to static and all works but there is a problem when instantiating more police cars that sometimes they will be instantiated inside other game objects in the map

the script that instantiate the police cars instantiate the cars based on distance that i took the player position and instantiate the enemy 20meter far from the player out of the camera view (transform.position.z-20.0f)

Comment
Add comment · Show 1
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 hexagonius · Jul 05, 2017 at 11:37 AM 0
Share

The Physics class has a lot of overlap functions. If you know the size of your cars, you can just do a cast at where you want the car to be spawned and see if it hits something. If not, the space is given.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ShadyProductions · Jul 02, 2017 at 07:52 PM

If you store every created car in a list of gameobjects you can look through the list if one of the gameobject's position is equal to the position you're trying to instantiate then you cancel and relook for a new position.

If you also include obstacles and are certain that no new obstacles will be created you can retrieve all gameobjects and cache them and then look through all obstacles position aswel and compare

Here is the good caching version for all gameobjects (including existing police cars)

     private GameObject[] _obstacles;
     public GameObject[] Obstacles
     {
         get
         {
             if (_obstacles != null) return _obstacles;
             _obstacles = GameObject.FindObjectsOfType<GameObject>();
             return _obstacles;
         }
     }

However know that this will not retrieve new created police cars or obstacles, only the existing ones up to the point you call the obstacles property. So you will need to work with lists for that.

Comment
Add comment · Show 4 · 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 Hamilcar-games · Jul 04, 2017 at 12:36 PM 0
Share

the obstacle are already in the map

avatar image Hamilcar-games · Jul 04, 2017 at 12:39 PM 0
Share

the system that working on is like the smashy road game if you know it but designed for pc with better graaphics that i have prepared in maya

avatar image Hamilcar-games · Jul 05, 2017 at 11:18 AM 0
Share

if you can explain me more the code and how to cancel the instantiate when there is something

avatar image ShadyProductions Hamilcar-games · Jul 05, 2017 at 11:23 AM 0
Share

I've given you what you need.

 if (!Obstacles.Any(f => f.transform.position == PositionYouWantToInstantiateOn)) 
 {
 //instantiate
 }

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

120 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

Related Questions

Need help detecting barriers for my game 0 Answers

NavMesh giving jerky like motion 0 Answers

Rotating the a NavMeshAgent when going up- or downhill? 1 Answer

Make AI follow player without jumping about 0 Answers

Connecting two navmeshes without "Speedboost" 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