instantiate prefabs to be sent in different directions
im using
void SpawnCoin(Vector3 spawnPosition)
{
Instantiate(coinPrefab, spawnPosition, coinPrefab.transform.rotation);
}
and calling it from a for loop
ContactPoint contact = collision.contacts[0];
Vector3 pos = contact.point;
for (int i = 0; i < amountOfCoins; ++i)
{
SpawnCoin(pos);
Debug.Log(i);
}
how do i make each item that is spawned explodes in different directions from that point
Answer by Taxen0 · Feb 02, 2016 at 03:32 PM
If the coinPrefab always get spawned like this and should behave like this everytime you could simply add some code in the coins script to send it flying whenever it is created, like in the Start() method.
If you need more control over when the coin flies away (maybe you also have coins that should be stationary in the level) you could do something like this:
void SpawnCoin(Vector3 spawnPosition)
{
GameObject coinObj = (GameObject)Instantiate(coinPrefab, spawnPosition, coinPrefab.transform.rotation);
//get the coin script
CoinScript coin = coinObj.GetComponent<CoinScript>();
//send the coin flying
coin.SendFlying();
}
Change "CoinScript" so it matches the name of the script attached to the coin prefab.
To make the coin actually fly away the best way depends on the type of game you have, but you could for example use Unity's physics and add a force pulse in a random direction.
Yeah okay after a lot of googling and trial and error I believe this to be best practice thank you very much