Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to make 3d objects randomly appear in a 3d environment ?

Discussion in 'iOS and tvOS' started by aakaash, Jul 12, 2009.

  1. aakaash

    aakaash

    Joined:
    Jun 28, 2009
    Posts:
    38
    i have a particular 3d model which i want should appear randomly in x,y,z position in a preset area. is it possible to do so ??? if yes how ???

    ps - dont even mind random 2d objects. Thanks for all the help
     
  2. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Not sure what you mean by this sentence, but random is very much doable for anything you can do that is preset. For instance, if you want to create a random object, first create an array of GameObjects and put all the objects that CAN be created into there and when you instantiate your object, just say:

    Code (csharp):
    1.  
    2. var newObj : GameObject = ObjArray[Random.Range(0, ObjArray.length)];
    3.  
    Done.

    If on the other hand you want random locations, just do:
    Code (csharp):
    1.  
    2. var newLoc : Vector3 = Vector3(Random.Range(0, 100), 0, Random.Range(0, 100) );
    3.  
    Done.

    On the other hand, if you want the characters to appear at the same positions in your map, but to choose a random location out of those... For example for spawn points where the enemy can start either behind the wall or inside the bunker or on top of the tower etc etc. You define WHERE the enemy can start, but the enemy must choose WHICH of those locations to start from...

    Just create empty GameObjects and put them in your scene where you want it to be and then create an array of Transforms and drag them into there. Now just use the code from my first example to choose a location and you are set.

    So, for example:
    Code (csharp):
    1.  
    2. var SpawnPoints : Transform[];
    3. var Objs : GameObject[];
    4.  
    5. function Spawn()
    6. {
    7. var obj : GameObject = Objs[Random.Range(0, Objs.length)};
    8. var pos: Transform = SpawnPoints[Random.Range(0, SpawnPoints.length)];
    9.  
    10. Instantiate(obj, pos.position, pos.rotation);
    11. }
    12.  
    13.  
    Done. A random Object appearing at a Random position...

    Was that what you wanted?
     
  3. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Re-reading your post it sounds like you want a random time also...

    This is a bit more tricky and there is two ways for you to so it.

    You could create a function that contains a:
    Code (csharp):
    1.  
    2. yield WaitForSeconds(Random.Range(0.0, 10.0));
    3.  
    But I am not sure how to call the function again, automatically.

    I love using:
    Code (csharp):
    1.  
    2. function Create()
    3. {
    4.    InstantiateMyObj();
    5.   Invoke("Create", Random.Range(0.0, 10.0));
    6. }
    7.  
    but I was recently told that the Invoke command is not a nice command to use. Personally I have never had any problems with it and I use it EXTENSIVELY for creating randomness in my projects and I have never run into any problems with it except when using a yield in the same function. That said I also run into problems when I use two yield statements in the same function...

    Which ever you use is up to you, but those are the options...
     
  4. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    513
    "create an array of Transforms and drag them into there."

    How do you do that?
     
  5. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Create a script and inside that script write:

    Code (csharp):
    1.  
    2. var myObjects  : Transform[];
    3.  
    Now, once you have that script on a gameobject, select that game object and then in the inspector click on the little arrow beside "My Objects" and give it a value of 5 or 10 or whatever you want. Next, simply drag any game object that is in the scene onto one of the 5/10/whatever spaces you have just created.

    Voila :)
     
  6. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    513
    Thanks very much - I'll give that a shot... I'm still such a noob to all this!

    P.S. In your code above...
    Code (csharp):
    1.  
    2. var obj : GameObject = Objs[Random.Range(0, Objs.length)};
    3.  
    There should be a "]" at the end of the line not a "}"

    Ta.
     
  7. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Typo. You are right, yes.
     
  8. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    513
    Okay now I'm feeling like an utter twit.

    I did as you say, yet I still can't get this to function.

    This is the first ever time I've attempted anything on Unity (beyond just placing scenes, loading scenes etc) and I feel so out of my depth here. But, it's the first part of the game mechanic for the game I've designed, so thought it would be a good starting point for me - one thing at a time :)


    So, to give an overview. I have 3 enemy types whom i want to spawn randomly at 5 spawn points that i have represented as empty objects in the scene and tagged as "Respawn'.

    I have created a placeholder enemy for now - just a box - for the sake of getting code right first. I've called this enemy VillagerRed and I have him in the scene off to the side out of the camera view.

    So, I've taken your code and have applied the "Respawn" script (the one starting above with varSpawnPoints: Transform{} ending in intantiate(obj,pos.position,pos.rotation);). I have assigned 5 Spawn Points and for the Objs I selected each of the 5 Spawn Point objects in the scene.

    Then I took your last bit of array script and for now have just applied it to the first spawn object (called Spawn1) in the scene. For Objects I selected Size of 5 and assigned each of the 5 spawn objects to each element. Something tells me this is the part that I have wrong.

    Or is it all of it?

    Please don't be afraid to say 'you idiot you've got it all wrong!" LOL :p

    Thanks again for your time.

    Noober
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You idiot, you've got it all wrong!

    That's not necessarily true, I just enjoyed taking you up on your kind offer ;-) You haven't actually mentioned what is going wrong with the code you are using - perhaps you can give a bit more detail?
     
  10. timsvw

    timsvw

    Joined:
    Aug 18, 2009
    Posts:
    42
    Just along for the ride.
     
  11. yannminh

    yannminh

    Joined:
    Jul 1, 2007
    Posts:
    43
    Hello, I was seeking for a script to do that... thanks very much, it is really nice...

    I am a newbie in scripting, but, it seems that the script need a little modification to work perfectly...

    the function "Spawn" does not seems to exist... so, it is necessary to replace it by "Awake" or "Start"...

    so the script should be IMHO :

    Code (csharp):
    1. var Objs : GameObject[];
    2. var SpawnPoints : Transform[];
    3.  
    4. function Awake ()
    5. {
    6. var obj : GameObject = Objs[Random.Range(0, Objs.length)];
    7. var pos: Transform = SpawnPoints[Random.Range(0, SpawnPoints.length)];
    8.  
    9. Instantiate(obj, pos.position, pos.rotation);
    10. }
    Hope I help... and thanks again for sharing...
     
  12. ParadigmGamingStudio

    ParadigmGamingStudio

    Joined:
    Jan 12, 2012
    Posts:
    1
    You really should probly look into this before you ask a question. Here is the EXACT code on the unity scripting manual

    Code (csharp):
    1. // Instantiates prefab somewhere between -10.0 and 10.0 on the x-z plane
    2. var prefab : GameObject;
    3. function Start () {
    4. var position: Vector3 = Vector3(Random.Range(-10.0, 10.0), 0, Random.Range(-10.0, 10.0));
    5. Instantiate(prefab, position, Quaternion.identity);
    6. }