Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Oreo14791999 · Jul 04, 2013 at 11:10 PM · spawnspawningcube

How to make an object spawn in different positions?

I have a script that spawn my cube in the same place i want it to spawn at different positions every time ? how do i do this ? Thanks in Advance!!

 var prefabToSpawn:Transform;
 
 var spawnTime:float;
 
 var spawnTimeRandom:float;
 private var spawnTimer:float;
 
 function Awake ()
 { 
                
                ResetSpawnTimer();
 }
 
 function Update()
 {
             
             if(spawnTimer > 0)
             {
                  
                   spawnTimer -= Time.deltaTime;
                   
                   if(spawnTimer <= 0.0)
                    {
                        
                        spawnTimer = 0;
                         
                          Instantiate(prefabToSpawn, transform.position, Quaternion.identity);
                 
                 ResetSpawnTimer();
                       }
             }
 }
  
 function ResetSpawnTimer()
 {  
        
         spawnTimer = spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0;
 }
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 Sjiggle · Jul 04, 2013 at 11:36 PM

If you want randomness, try this:

 Instantiate(prefabToSpawn, transform.position + Vector3(Random.Range(0f, 10000f),Random.Range(0f, 10000f), Random.Range(0f, 10000f)), Quaternion.identity);
 

this essentially adds a random Vector3 to the original transform position of the gameobject you attach this script to. For a more accurate random (yet pregiven) spawn, use an array of spawn points (vector3s) and do get a random index from those spawnpoints to spawn on to.

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 fafase · Jul 04, 2013 at 11:39 PM 0
Share

Why using transform.position + Vector3? Your range is quite big enough. By the way it is too big. Using those values will throw a warning. But the idea is there.

Also, you may want to check if the position is valid, what if your spawn is underground? Best is not totally random by using an array of predefined points.

avatar image Sjiggle · Jul 04, 2013 at 11:41 PM 0
Share

The question states: I have a script that spawn my cube in the same place i want it to spawn at different positions every time ? how do i do this ? Thanks in Advance!!

He wants to spawn at different points. Think of it as pseudo code.

avatar image
0

Answer by trs9556 · Jul 04, 2013 at 11:27 PM

Line 26 reads as followed:

  Instantiate(prefabToSpawn, transform.position, Quaternion.identity);

The part that says transform.position is the location where prefabToSpawn will spawn. Change this value and you'll get a different location.

You can do something along the lines of what you did in your ResetSpawnTimer() method.

Create a random range to "offset" the position.

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

If you need further help let me know.

EDIT

 var prefabToSpawn:Transform;
 
 var spawnTime:float;
 
 var spawnTimeRandom:float;

 var minimumXValue : float;
 var maximumXValue : float;
 var minimumYValue : float;
 var maximumYValue : float;
 var minimumZValue : float;
 var maximumZValue : float;
 private var spawnTimer:float;
 
 function Awake ()
 { 
 
     ResetSpawnTimer();
 }
 
 function Update()
 {
 
     if(spawnTimer > 0)
     {
 
          spawnTimer -= Time.deltaTime;
 
           if(spawnTimer <= 0.0)
           {
 
                spawnTimer = 0;

                var offset : Vector3; //a vector3 is (x, y, z)
                offset = new Vector3(Random.Range(minimumXValue, maximumXValue), Random.Range(minimumYValue,maximumYValue), Random.Range(minimumZValue,maximumZValue));
                Instantiate(prefabToSpawn, transform.position + offset , Quaternion.identity);
 
                ResetSpawnTimer();
           }
     }
 }
 
 function ResetSpawnTimer()
 {  
 
     spawnTimer = spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0;
 }
Comment
Add comment · Show 8 · 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 Oreo14791999 · Jul 04, 2013 at 11:31 PM 0
Share

thank you very much i think i can do it myself if not is it ok if i contact you ? Thanks Again!

avatar image trs9556 · Jul 04, 2013 at 11:38 PM 0
Share

By all means you are more than welcome to contact me if you need help, and or post it in the comments.

What @Sjiggle just posted was just about exactly the same thing I was going to. Except ins$$anonymous$$d of the range being (0, 10000) I was going to make vars that you can set in the inspector that way you have control over the area it spawns.

avatar image Sjiggle · Jul 04, 2013 at 11:40 PM 0
Share

Which of course is a better solution :)

avatar image Oreo14791999 · Jul 04, 2013 at 11:49 PM 0
Share

Any chance one of you will do it for me I am really under pressure and i dont have the time thank you !

avatar image Oreo14791999 · Jul 05, 2013 at 12:11 AM 0
Share

bump i have tried it myself, it hasn't worked to well for me how do I set it so it spawns in a certain area ?

Show more comments

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

17 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

Related Questions

Spawning Cube on empty gameobject 1 Answer

Random cube length on instantiate... 1 Answer

Respawn after falling off script? 2 Answers

Spawning in Specific Areas 1 Answer

How can I spawn another game object once one has been destroyed? 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