- Home /
Random Genration of Cube
Hii, i want to make random generation of cubes.I have number of n cubes with different materials applied to each.
Means suppose i 'm having 10 cubes in one scene, and in in second or in other scenes , i need to display random cubes from first scene . so how should i do that?
i'm a flash developer, so i know how to generate random images using following way.
Code:
function randomGen():String{
var labels=new Array("l1","l2","l3"); //(here que1,que2...etc are frames labels on which i have taken 3 images on each frame
var index:Number=Math.floor(Math.random() *labels.length);
return labels[index];
}
this.gotoAndStop(randomGen());
similar to this how can i generate random cubes in unity.?
**There is one common thing in all cubes is a custom tag. All cubes are having same custom tag.
Any help will be helpful.
Thanks.
Answer by Statement · Mar 28, 2011 at 12:10 PM
I'm not quite sure what you mean by a random cube. There isn't many ways a cube can be random except materials applied to it? :) I don't know why you'd want the cubes from the first scene, is this some sort of palette? Maybe you can just make use of prefabs?
// In inspector, assign any cube prefabs here. var cubePrefabs : GameObject[];
// Call this function to create a random cube. function InstantiateRandomCube() { var prefab = cubePrefabs[Random.Range(0, cubePrefabs.Length)]; return Instantiate(prefab); }
i mean to say i have number of n cubes with different materials applied to each .
Actually i 'm trying to make a small memory game. each time when u will start game u will get random cubes, and u hav to remember these cubes to complete the level.
in level u will show number of cubes , from that u hav to identify correct one.
so wht will be the good way to achieve it?
Well I am not quite sure how you envision your game. But I'd start off by making prefabs for all the different types of cubes. Say you create a 5x5 grid of random cubes from these prefabs, show it for 60 seconds. Darken the screen for 30 seconds, then replace a few of them. When user clicks a cube that already was there, they get a penalty. When user clicks all cubes that are new, the next level loads. Something like that?
yah, similar to this, but the level should be lik this way:
suppose for first level if u given a cube of white color.it will display for 30 sec. then u will get new screen and in that u will be shown 5 cubes, from that u just need to choose correct one cube(i.e) white color cube. and the next level will be loaded. and so on...
Yeah so you can start off with code supplied above. You could add code that creates a random prefab and store which prefab it was in a variable. Then in your On$$anonymous$$ouseDown you can compare materials to see if it was the same object that was clicked.
Answer by AngryOldMan · Mar 28, 2011 at 12:34 PM
Similar to what Statment said, what do you mean by random?If you want ten randomly sized cubes in then something like
var CubePrefab : GameObject; var AmountOfCubes : int;
function Update () { if (AmountOfCubes <=10) { SpawnRandomCube(); } }
function SpawnRandomCube() { var RandomCube = Instantiate (CubePrefab,transform.position,transform.rotation); AmountOfCuces = AmountOfCubes + 1; }
attach that to an empty gameobject where ever you want cubes to spawn, but they all spawn in the same place at the same time.then on the Cube~Prefab attach a script that says something like
function Awake ()
{
var SingleRandomSize = Random.Range (1,10);
tranform.localScale = Vector3 (SingleRandomSize,SingleRandomSize,SingleRandomSize);
}
or if you wanted them to spawn in random places then on your empty gameobject put
var CubePrefab : GameObject; var AmountOfCubes : int; var RandomPos : int;
function Update () { if (AmountOfCubes <=10) { RandomPos = Random.Range (1,20) SpawnRandomCube(); } }
function SpawnRandomCube() { tranform.postition = Vector3 (RandomPos,RandomPos,RandomPos); var RandomCube = Instantiate (CubePrefab,transform.position,transform.rotation); AmountOfCuces = AmountOfCubes + 1; }
if you want to be more specific about what you're trying to achieve you will get more specific help.
yes i just wanted to put them in random places after each time when u will play this game.
ok i'm trying to apply above code.bt may i'm doing in wrong way.
i'll just tel u wat i have done .
have created a prefab (from maya). and in each scene(or say level) i'm placing number of cubes on scene from that prefab.
2.the i have created a empty gameobject.and attached a cube detection script. That means if i'm having 2 cubes in scene, and if i want to go to next level, i need to click on correct cube, and if u click correct cube , u will post to next level.(for choosing of correct cube i have used custom tag, and raycast concept).
3.now whenever i'll play this game i should feel that its a new game,as we r going to put each time random cubes(with different "materials"), so it will seems new game. so how should i use this code.?
don't it's example code and wont work properly, you should learn to code and use this as a guide to make your own script.
thanks for these code it really was help full to me i have some more question. what if i want to move those random objects in the same range of x actually i want to create a bike game and i want random car in Terrain and i want to move those car in random manner what should i do plz help :(
Your answer
