- Home /
Add Explosive Force - Coins
Hello,
I have a bunch of gold coins that initiate when I kill an enemy.
I want to be able to have the coins disperse from the kill point (like an mini-explosion), and then after a few seconds, the coins move towards the player.
So I have this function that is called when the enemy die:
function InitCoins(){
var coins = new Array();
for(var i = 0; i < 5; i++){
var coin : Transform;
coin = Instantiate(coinPickup, transform.position, transform.rotation);
coin.gameObject.GetComponent(CoinPickup).player = target;
coin.gameObject.GetComponent(CoinPickup).timer = (i);
var radius = 10.0;
var power = 100.0;
var explosionPos : Vector3 = coin.transform.position;
coin.rigidbody.AddExplosionForce(power, explosionPos, radius, 10.0);
}
}
The explosion force doesn't seem to do anything? I have the timer and move to player sorted:
function Start () {
var random = Random.Range(4, 7);
yield WaitForSeconds(timer / random);
canMove = true;
}
function FixedUpdate () {
if(canMove){
var lookPos = player.transform.position - transform.position;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 10);
//Move towards player - via transform, or rigidbody
//transform.Translate(Vector3.forward * speed * Time.deltaTime);
//rigidbody.AddRelativeForce (Vector3.forward * 10);
}
}
I mainly just want to get the mini explosion effect working to begin with.
Things to note: The game is a top down game, so all Y movements are blocked, as well as X and Z rotations.
Answer by robertbu · Jun 30, 2013 at 02:54 PM
If you left the mass of your coins at 1.0, a force of 100 will not overcome gravity. In addition, because you are applying the force at the position of the object (and using upwardsModifier), you can get the same effect by:
rigidbody.AddForce(Vector3.up * 500.0);
That is, you are not getting the use of a linear dropoff in force because the explosion is happening at the position of the coin.