- Home /
Duplicate Question
random object creation and there movement
i have created random object of type cube and when i run it gets created now i want those created objects to have motion in x and z axis,any on eof u know how to do that
Here is my code
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;
}
now i have to move these cubes in z axis and if possible then in z axis.
suggest you read articles on unityGE$$anonymous$$S.com for many beginner articles on $$anonymous$$OVE$$anonymous$$ENT.
you can use a rigidbody and apply force, or you can animate the object (move it yourself) using Translate or just setting the position
search on here for literally 1000s of answers on it, good luck!