- Home /
Spawning coins randomly in a 3d infinite runner game
I have a 3d infinite runner car racing type game in which the player is stationary and the background moves. In my game I want to spawn coins randomly over time and the coins has to be spawned very much ahead of the player, and the z axis of the coins get reduced keeping y axis constant and x axis values in a random range of -2 and 2. The coins are spawning correctly but they are spawned in an irregular manner. I created four coin game objects in my scene, I want to spawn the 4 coins in a straight line because then the player can easily collect the coins since they are coming in a straight line to the player. The players motion is only in the x axis from -2 to 2. Now my problem is that the coins are spawned irregularly because of that the coins cannot be collected easily by the player. THis is my code:
function Update()
{
MoveCoin();
}
function MoveCoin()
{
ReleaseCoin();
//CoinsOnRoad is an array containing the current coins which are on the road
//CoinPool is the array of coins
for(var i:int =0;i<CoinsOnRoad.length;i++)
{
var gcoin:GameObject = CoinsOnRoad[i] as GameObject;
gcoin.transform.position.z-=3*speed*Time.deltaTime;
if(gcoin.transform.position.z>=-10)
{
//Do nothing if the coin is on the visible area of the road. If it becomes invisible
//remove the coins from CoinsOnRoad Array and insert the coin back to the CoinPool Array
}
else
{
CoinPool.push(gcoin);
CoinsOnRoad.remove(gcoin);
}
}
}
function ReleaseCoin()
{
if(CoinPool.length==0)
{
}
else
{
var coin:GameObject=CoinPool.shift() as GameObject;
CoinsOnRoad.push(Instantiate(coin,new Vector3(Random.Range(-2.0,2.0),0.3,30+Random.Range(1,10)),Quaternion .identity));
}
}
The coins are spawning correctly but in an irregular order. Can someone please help me out? Thanks in advance..Since I'm new to unity, I didn't knew whether even my game logic is correct or not. Can some one correct me with the code if I'm wrong some where in my code.
try to hold a z variable in memory and increment it everytime you spawn a coin, then ins$$anonymous$$d of passing a random number to the instantiate method you can increment the z variable and have the coins in a nice straight line, you can also do some other nice stuff if you only choose a random number for the first part of the x axis, such as diagonal lines and other patterns, by incrementing a value after the first one has been randomly chosen
Answer by hav_ngs_ru · Oct 31, 2014 at 04:23 PM
var randomX;
var coinCount = 0;
var coinNum = 0;
var coinLineMin, coinLineMax; // setup max and min coins in one line
var coinInterval; // distance between coins in line
function ReleaseCoin()
{
if(CoinPool.length==0)
{
}
else
{
if(coinNum == coinCount) {
randomX = Random.Range(-2.0,2.0);
coinCount = Random.Range(coinLineMin, coinLineMax);
coinNum = 0;
}
coinNum ++;
var coin:GameObject=CoinPool.shift() as GameObject;
CoinsOnRoad.push(Instantiate(coin,new Vector3(randomX,0.3,30+coinNum*coinInterval),Quaternion .identity));
}
}
somethink like this.
I didnt test a code and I usually use C#, so stupid mistakes are possible :)
but i hope idea is clear :)
Thanks for the answer. I have one doubt, can you please clear me. How do I delete the coin game object created using the Instantiate after the coin leaves my camera visibility area?
you didn't used the randomZ variable anywhere in the code? Where to use that??
make coin controller in separate script, and make void OnBecameInvisible() function, it will be called every time a coin goes out of screen. And just call Destroy() in it to destroy coin.
BUT!
Try to not use Instantiate and Destroy when playing a level, it may cause a hangs. Better way - is to Instantiate a objects in Start or Awake, and just Activate/Deactivate it when you need it to appear/disappear. Find an ObjectPool scripts to see an example of how to manage objects.
Bear in $$anonymous$$d that if your object is visible in Editor windos (when you play your game in unity Editor) - the OnBecameVisible will call at start of game for this object (not when it addears in $$anonymous$$ainCamera`s screen), and OnBecameInvisible will NOT call, despite it hides from a $$anonymous$$ainCamera`s screen. So hide editor window (or move it viewport to NOT to see coins) when you debug game.
As alternative way is to check distance between camera and coid to deter$$anonymous$$e when a coin is far away and not needed. This way will not cause features like in item "2". :)
Your answer
Follow this Question
Related Questions
Spawning infinite coins 1 Answer
Infinite runner spawning enemies, need some guidelines. 0 Answers
How Increase And Decrease And Reset Spawn Rate Over 0 Answers
,Getting a Spawner Manager Script to reference a Spawner 1 Answer
How to make enemies spawn IN the ground without rising up automatically first? 1 Answer