void SpawnCoins()
{
for (int i = 0; i < coinDrops; ++i) //Spawns an amount of coins
{
GameObject coin = Instantiate(coinPrefab, transform.position, Quaternion.identity) as GameObject;
Vector3 targetPosition = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), 0);
Rigidbody2D rb = coin.GetComponent<Rigidbody2D>();
rb.AddForce(targetPosition * speed);
Debug.Log("Adding force");
if (Vector2.Distance(coin.transform.position, targetPosition < 1f) //this part isn't working
{
rb.velocity = Vector2.zero;
Debug.Log("It's going through.");
}
}
}
Also, tried:
if (coin.transform.position == targetPosition)
{
rb.velocity = Vector2.zero;
Debug.Log("It's going through.");
}
So, I’m spawning coins after an enemy dies. After the coins instantiate, I want them to burst out in random directions and move a small distance away, and then stop. I can get a set amount of coins to spawn and they do move in random directions…but they won’t stop moving. I would like help to figure out how to stop the coins.
I’ve used a debug.log to check if the “if” statement was going through, and it wasn’t.
I’ve also tried moveTowards, but the coins just teleport to the target position. I’m guessing this is because it’s not being called in the Update function. But I’m not sure how to reference the instantiated coin gameobject outside of it’s own function. When I made it public, I got errors.
Any help/suggestions would be very much appreciated!