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 MSSK15 · Jan 10, 2019 at 06:01 AM · unity 5instantiatespawning problemsspawning-enemiesspawn-when-click

In a closed room Instantiate game objects around the player randomly.

Hi, I want to instantiate objects around the player. Here player will be in a closed room. if the player is near the wall i want to instantiate objects on the other side of player and none of the objects should not collide each other. instantiated objects will 1-20. In any case objects should not instantiate outside the room. i have room like this . any help would be grateful. thanks in advance.

room-preview.jpg (233.5 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

1 Reply

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

Answer by tormentoarmagedoom · Jan 10, 2019 at 12:40 PM

Good day.

Then you first need to generate random positions to spawn objects.

To create positions, you should use something like this (i do an example in 2D for easy understand, you should do it with Vector3):

Lets imagine your area limits are from x=-10 to x=10 and from y=-20 to y=20

 Vector2 possiblePosition = new Vector2 (Random.Range(-10,10), Random.Range(-20,20);

This will generate a random position inside your area. Now you need to know if is not too much close to any other object. So, you need something to detect other objects. Best way I think is to make all instantiated objects have the same TAG, so you can easy find them, for example "InstObj". And the player lets say have the TAG "Player"

So, when you instantiate the firstone, only need to check for the player position. I normally use the Distance function. If i detect the distance between the player and the new object is less than 2, i will need to create a new random position, until i get a random position away from other objects.

 Vector3 FindGoodPosition()
 {
 Vector2 possiblePosition = new Vector2 (Random.Range(-10,10), Random.Range(-20,20);

 while (Vector3.Distance( GameObject.FindObjectWithTag("Player").transform.position, possiblePosition) <2)
  {
  possiblePosition = new Vector2 (Random.Range(-10,10), Random.Range(-20,20);
  }
 }

This way you will have a position always inside the area and at leas 2 units away from the player. Now lets check with all other objects

 foreach (GameObject ObjectToCheck in GameObject.FindObjectsWithTag("InstObj"))
 {
 if (Distance between possiblePosition and ObjectToCheck .transform.position <2) 
 {
 return = null;
 }
 }
 return possiblePosition;
 }

This way this methos will only return a position if its correct, if not, it will return null, so to instantiate the objectit, you must do something like this:

 Vector3 Positiontospawn = FindGoodPosition();

 while (Positiontospawn == null)
 {
 Positiontospawn = FindGoodPosition();
 }

 Instantiate (ObjectPrefab, Positiontospawn, rotation);


Good Luck!

PD: This is a guide, not a script to copy paste, try to understand ALL it does, or you will never succeed in game develop.

Bye!

Comment
Add comment · Show 1 · 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 MSSK15 · Jan 11, 2019 at 11:23 AM 0
Share

thank you, ill try this:)

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

203 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

Related Questions

Question about Unet and spawning 0 Answers

instantiate spawning meany objects in multiplayer 0 Answers

Instantiating unique Prefabs to unique child objects in C# 3 Answers

Object Instantiate 1 Answer

Create random order and go through till the end (C#) 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