In my game of infinite runner type game, My hero gameobject is a car. The hero can move only in x axis from -2.0 to +2.0 only. The hero is not moving forward, instead the road and the other game objects are moving towards the hero so the motion is only for the other game objects. In the road I have several coin game objects as collectibles for the hero for level up. There are actually 12 coin game objects in my scene and as the game starts these coins are placed in and array called CoinPool. From these CoinPool as the game starts the coins are to be released to the road at certain time intervals at a random numbers. I have a release coin function, move coin function and remove coin function in my script. The release coin function will release the random number of coins in the game. These random number of coins are taken from the CoinPool and then it is then pushed to another array called CoinsOnRoad array. After releasing the coins in the road, the coins are pushed into the CoinsOnRoad array from The coinpool array. In the move function, the coins are moved by moving the z coordinates of the coins. When the coins move from the view area of the camera it is then removed from the game area using the remove function.
Now coming to the problem, My coins are successfully releasing on the roads, suppose I released 5 coins in the road, and then my move function executes and instead of moving all the coins in a steady speed, the first x coins are moving at a very much higher speed than the other 5-x coins. So it gives an bad feeling to the game. Can some one please check my code and correct me if I had done any mistake in the code?
function ReleaseCoin()
{
if(CoinPool.length==0)
{
}
else
{
coinnum=0;
var coin:GameObject;
randomX=Random.Range(-2.0,2.0);
randomZ=Random.Range(1.0,5.0);
coincount=Random.Range(4,10);
while(coinnum<coincount)
{
coin=CoinPool.shift() as GameObject;
coin.transform.position.x=randomX;
coin.transform.position.y=0.3;
coin.transform.position.z=30-randomZ-Random.Range(7.0,10.0);
CoinsOnRoad.push(coin);
coinnum++;
}
}
}
function MoveCoins()
{
if(cointime<=0 && CoinsOnRoad.length<=0)
{
ReleaseCoin();
cointime=Random.Range(4,10);
}
else if(CoinsOnRoad.length>0)
{
for(var i:int =0;i<CoinsOnRoad.length;i++)
{
var gcoin:GameObject = CoinsOnRoad *as GameObject;*
_ gcoin.transform.position.z-=62Time.deltaTime;_
-
if(gcoin.transform.position.z>=-10)*
-
{*
-
}*
-
else*
-
{*
-
CoinPool.push(gcoin);*
-
CoinsOnRoad.remove(gcoin);*
-
gcoin.transform.position.x=15+Random.Range(0,15);*
-
}*
-
}*
-
}*
-
cointime-=Time.deltaTime;*
}
function RemoveCoins(removecoin:GameObject)
{ -
CoinPickUpSound.audio.Play();*
-
CoinPool.push(removecoin);*
-
CoinsOnRoad.remove(removecoin);*
-
removecoin.rigidbody.velocity=Vector3.zero;*
-
removecoin.transform.position.x=15+Random.Range(0,15);*
-
removecoin.rigidbody.drag = 3;*
}