• 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 N00MKRAD · Jul 13, 2014 at 04:21 PM · gameobjectlocation

[Solved] Make GameObjects spawn at custom locations (Slender like)

(Sry, maybe my English isn't perfect) Can I make multiple gameObjects spawn at custom locations, with custom rotation? Or is it easyer to put all of them to their location, but only activate 8 random picked ones when the scene starts?

I want to do that like in Slender (there are 10 possible locations but only 8 pages). Thanks!

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 MarioAGOrdiano · Jul 13, 2014 at 08:07 PM 0
Share

Could you provide a code example?

or pseudocode at least

also if your native language is either spanish or german pm me and I can help.

2 Replies

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

Answer by N00MKRAD · Jul 14, 2014 at 02:53 AM

Ok, I found a really good script for it (AlucardJ´s Slender Guide)

You can set the spawnpoints (as much as you want) and set multiple pages (gameObjects, not prefabs) to spawn.

 #pragma strict
 import System.Collections.Generic; // to use List
  
 public var spawnPoints : Transform[];
 public var paperPrefabs : Transform[];
  
 private var availableSpawnPointsList : List.< Transform >;
 private var spawnList : List.< Transform >;
  
  
 function Start()
 {
     ChooseRandomSpawnPoints();
     SpawnPapers();
 }
  
  
 function ChooseRandomSpawnPoints()
 {
     var i : int = 0;
    
     availableSpawnPointsList = new List.< Transform >();
     spawnList = new List.< Transform >();
    
     // add all spawn points to a list
     for ( i = 0; i < spawnPoints.Length; i ++ )
     {
         availableSpawnPointsList.Add( spawnPoints[i] );
     }
    
     // choose a different random position for each paper prefab
     for ( i = 0; i < paperPrefabs.Length; i ++ )
     {
         var rndPos : int = Random.Range( 0, availableSpawnPointsList.Count );
        
         spawnList.Add( availableSpawnPointsList[rndPos] ); // add this to the spawn point list
        
         availableSpawnPointsList.RemoveAt( rndPos ); // remove this from the available points list
     }
    
 }
  
  
 function SpawnPapers()
 {
     // spawn paper at each location
     for ( var i : int = 0; i < paperPrefabs.Length; i ++ )
     {
         var paper : Transform = Instantiate( paperPrefabs[i], spawnList[i].position, spawnList[i].rotation );paper.name = "Paper";
         paper.name = "Paper";
     }
 }

It works fine, but I´ll try @MagicoCreator´s script, too.

Comment
Add comment · 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
1

Answer by MagicoCreator · Jul 13, 2014 at 10:54 PM

you can define an array of Vector3s (or GameObjects) and shuffle the array, and instanctiate at the first X elements with Random rotation: for example: GameObject[] Positions;

 function Start(){
   //Shuffle the array
     for (int t = 0; t < Positions.Length; t++ )
     {
         GameObject tmp = Positions[t];
         int r = Random.Range(t, Positions.Length);
         Positions[t] = Positions[r];
         Positions[r] = tmp;
     }
   //instantiate your objects
   for(int i = 0; i < howmanyyouwanttoinstantiate; i++){
     Instantiate(YourObject,Positions[i].transform.position,Random.rotation);
   }
 }

if you want to randomize the rotation only on Given axes you can use

 Quaternion.Euler(Random.Range(0,360),Random.Range(0,360),Random.Range(0,360));

instead of Random.rotation, you can replace the Random.range with the number you want on the axis

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 N00MKRAD · Jul 14, 2014 at 02:39 AM 0
Share

Thanks! I found another good script at AlucardJ´s Slender Guide, but I´ll test which one is better.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i spawn a prefab at an empty game object? C# 2 Answers

How to swap 2 game objects from any position on a 2d game pad? 0 Answers

spawn an object at the location of an empty gameobject 1 Answer

Instantiating Multiple Game Objects to the Position of a Current GameObject 2 Answers

Best way to determine if there is a gameobject in multiple locations? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges