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
1
Question by shiranui93 · Oct 29, 2012 at 10:25 AM · fpsspawnbullettargetreplay

Random spawn points for my target prefabs?

I want the FPS mini game i'm making on Unity replayable, is there any way I can make my target prefabs spawn into random areas of the game?

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

Answer by fafase · Oct 29, 2012 at 10:26 AM

 GameObject obj = (GameObject)Instantiate(prefab,new Vector3(Random.Range(a,b),Random.Range(a,b),Random.Range(a,b)),Quaternion.identity);


Now you need to give values to a's and b's so that object does not get instantiated outside the boundaries of your game.

Edit: Instead of totally random, you could have an array of position that you randomly choose from. For instance, add an empty object to your scene, tag it as SpawnPoint. Create a prefab and drag and drop the empty object into it. Remove the original one from the scene.

Now from your new prefab start placing them in the scene where you would like your objects to be created. Place them high enough so that your newly instantiated object is not halfway through which would make it fall down below the terrain. You could also place them behind a wall or any environment thing that would hide the instantiation.

Now, in your object to be created add a script like this:

 Vector3[] = vecArray;
 int top; 
 void Start(){
     GameObject objs = GameObject.FindGameObjectsWithTag("SpawnPoint");
     foreach(GameObject obj in bjs){
          vecArray = objs.GetComponent<Transform>().position;
     }
     top = vecArray.Length;
     InvokeRepeating("CreateObject",0.01f,10.0f);
 }
 void CreateObject(){
     Instantiate(prefab,vecArray[Random.Range(0,top)],Quaternion.identity);
 }

Now I see coming the "I don't use C#" So same is:

 var vecArray:Vector3[];
 var top:int; 
 function Start(){
     var objs = GameObject.FindGameObjectsWithTag("SpawnPoint");
     for (var obj in objs){
         vecArray = obj.GetComponent(Transform).position;
     }
     top = vecArray.Length;
     InvokerRepeating("CreateObject",0,01f,10.0f)
 }
 function CreateObject(){
     Instantiate(prefab,vecArray[Random.Range(0,top)],Quaternion.identity);
 }

It will take generate a random value for the index of the array and will get the Vector3 stored there. InvokeRepeating will call the CreateObject function from the start of the scene and every 10s will spawn a new object.

Comment
Add comment · Show 3 · 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 GetColor · Jun 13, 2013 at 06:19 PM 0
Share

I was trying to use your C# code for my own project, and I ran into this error: error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for GetComponent' and no extension method GetComponent' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)

It doesn't like the GetComponent bit in the array assignment.

avatar image fafase · Jun 14, 2013 at 05:54 AM 0
Share

Yep modified

avatar image justasmig · Aug 11, 2015 at 06:50 PM 0
Share

I think there is a problem in c# code. In the 5th line "

foreach(GameObject obj in bjs){

" there is bjs ins$$anonymous$$d of objs. Am I right?

avatar image
0

Answer by shiranui93 · Oct 29, 2012 at 10:50 AM

Will I also need to put a y value as I don't want the target to go to high so the player can't hit it. Also is that Javascript?

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 adrenak · Oct 29, 2012 at 08:45 PM 0
Share

Please post as a comment unless its not an answer.

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

12 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

Related Questions

Rigidbody bullets won't spawn at gun barrel... 2 Answers

collider doesnt react to script 0 Answers

Vuforia, how to spawn a prefab on target image and not above? 1 Answer

Frame rate drops when replaying level 0 Answers

Troubles With A Shoot Script 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