- Home /
Spawning Coins infront of the player
I want to spawn coins in front of the players path (Right, left). Any ideas how should i do it? I am making a chunks of coins and spawn them when the player reaches a certain point, When ever the coins(chunk) are spawned, i feel a jitter on the screen like a minor halt while loading coins. Please also advice on instantiating prefabs on run time. What is the best possible way to do that, so that it wont compromise the game play. I am new to unity, just point me to the right direction. Thanks
Answer by aldonaletto · Jan 20, 2013 at 01:25 PM
Instantiate is a heavy operation: it allocates and initializes lots of memory blocks for each object created. When the object is destroyed, these blocks become useless, and at certain times another heavy operation called "garbage collection" must be executed by the system to make this memory available again.
In order to reduce these performance killer operations, you can create an "object pool" for the coins: it's a list that keeps the coin objects; instead of destroying a coin, deactivate it; instead of creating a new coin, re-activate one of the deactivated coins at the desired position/rotation.
An object pool can be easily implemented using List, like below:
import System.Collections.Generic;
var coinPrefab: GameObject;
var coinPool = new List.<GameObject>();
function IsCoinFree(coin: GameObject): boolean {
return !coin.activeInHierarchy;
}
// use this instead of Instantiate:
function InstantiateCoin(position: Vector3, rotation: Quaternion): GameObject {
var coin: GameObject = coinPool.Find(IsCoinFree); // is any free coin in the pool?
if (coin){ // yes, this coin is free:
coin.SetActive(true); // activate it...
coin.transform.position = position; // at the desired position...
coin.transform.rotation = rotation; // and orientation
} else { // no, all coins are in use:
// instantiate a new coin:
coin = Instantiate(coinPrefab, position, rotation);
coinPool.Add(coin); // and add it to the pool
}
return coin; // anyway, return the coin GameObject reference
}
// always use this to destroy the coin:
function DestroyCoin(coin: GameObject){
// just deactivate it - the coin now is free in the pool
coin.SetActive(false);
}
The idea is simple: when you want to create a new coin, call InstantiateCoin(position, rotation): this will first search in the pool for free coins; if there's a free coin (deactivated) in the pool, activate it at the desired position/rotation and return a GameObject reference to it; if there's none, create a new one as usual, add it to the pool and return its reference. When you don't need the coin anymore, "destroy" it using DestroyCoin(coin): the coin is simply deactivated, so that it disappears from the scene and become available in the pool (deactivated objects in the pool are considered available).
NOTE: If the coins have their own scripts, you may have to call a custom Start function to initialize their variables, so that a recycled coin behaves exactly like a brand new one.
Yeah, the OP should accept this answer. Really helped me out.
Do I add this to an empty game object / the player / coin?
Answer by cdrandin · Jan 20, 2013 at 11:09 AM
Assuming these coins of a sphere collider. Give some distance away from the player. Assuming the player is facing the z axis, standing vertical (y axis). So we want to spawn these coins along the x axis. Have some set distance, so, distanceFromPlayer which is on the z axis. So just give how long you want it to go for lets display 4. So divide that in half and take the ceiling, for uneven number of coins. Now just place them along as such Vector3(transform.position.x + distanceFromPlayer, transform.position.y, transform.position.z + location)
location depends on the radius of sphere collider. So, if the coin is 4 units longs. Then take increments of 4 going along the path, placing them neatly in front of the player. Obviously, right now it places it in 1 place, just modify it to make multiple ones and just move along side the x axis. You could also use Vector3.Lerp to have it move smoothly.
$$anonymous$$ore clearer picture of the problem
There are three lanes in which the character can run. player run straight towards Z-axes. On turns it runs towards X axis. Coins should be placed like bread crumbs, like a trail of coins in front of the player in advance.(like temple run, Epic knight)
I like the idea u shared but its most appropriate for spawning coins on random intervals. Unfortunately my problem is little different. Any help would be highly appreciated.
Thanks for giving the time & thought.