- Home /
random object creation
i m creating a bike race game where i have object cube and it is running fine ,now i want to create random bikes or cars on same track (with some movement) on the way of that object so that user can feel he is actually on the road when i do
right now my cube object in z direction i want my random object to move in same track with same direction(z)
Take a look at iTween. It will allow you to move object from one point to another. In addition it has limited path features. Take a look at the PutOnPath() and PointOnPath() functions.
Answer by Abhinash kumar · Mar 22, 2013 at 09:25 AM
// VARS FOR RANDOM OBJECT CREATION
// COPY FROM HERE TO END
var CubePrefab : GameObject;
var AmountOfCubes : int;
var RandomPosx : int;
var RandomPosy : int;
var RandomPosz : int;
function Start()
{
for(var i=0;i<200;i++)
{
if (AmountOfCubes <=200)
{
RandomPosx = Random.Range (20,100);
RandomPosy = Random.Range (0,0);
RandomPosz = Random.Range (0,2042);
SpawnRandomCube();
}
}
}
// Move object using accelerometer variables
var speed = 1;
var movey=1;
function Update () {
var dir : Vector3 = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) z plane has fixed value 1 which cause it to move in same direction
//but user can move in x direction with the phone acceleration sensor
dir.x = Input.acceleration.x;
dir.z = movey;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
// dir *= Time.deltaTime;
// Move object
transform.Translate (dir * speed);
transform.Translate(0, 0, 1);
}
function SpawnRandomCube()
{
transform.position = Vector3 (RandomPosx,RandomPosy,RandomPosz);
var RandomCube = Instantiate(CubePrefab,transform.position,transform.rotation);
AmountOfCubes = AmountOfCubes + 1;
}
can you reedit your code so it's readable?
you have 101011 on top there
Your answer
![](https://koobas.hobune.stream/wayback/20220613094817im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating random objects 2 Answers
Generating objects randomly, only one will Instantiate 0 Answers
Instantiate Random Object at Random Position 1 Answer
Beginner : Random gameobject from array 2 Answers