How do I add force in the on trigger

Hi, I have this simple problem, I have an object and a group of objects (prefab) named coin. All I have to do is addforce to the coins when they enter the main object.
The collision/trigger thingy works, when I change the
“coin.rigidbody.AddRelativeForce (0, power, 0);” line, it works so the problem is with that line.

var coin : GameObject;
static var speed : int;
var power;



function Start () {
InvokeRepeating("checking", 0, 0.3);
}

function OnTriggerEnter ( collision : Collider){

 
 coin.rigidbody.AddRelativeForce (0, power, 0);

}

function checking (){
power = speed/3;


}

(the var power is set in other script

are the coins who enter the trigger? then you could use function OnTriggerEnter ( collision : Collider){ collision .rigidbody.AddRelativeForce (0, power, 0); } And are you sure you are giving a value to power before you use it? Do you see errors in the console?

are you instantiating objects, because if you are it will name it Coin(clone) and not coin so you will have to make a script to rename it on start.

1 Answer

1

The problem is you want to add an instantaneous force, where AddForce is applied over time in physics. For an instantaneous force in your case of entering a trigger volume, you need to use ForceMode.Impulse :

rigidbody.AddForce( transform.up * power, ForceMode.Impulse );

Links :

there is just one slight error, it moves the main object instead of the coins :D. How do I swap them?

use coin.rigidbody.AddForce( transform.up * power, ForceMode.Impulse );

okay, it is working when I set the coin var to a certain object already in game. Problem is, The coins are spawed randomly with a script, so when they are spawned, they dont react with the main object. I forgot to mention that, sry. And another thing is that Iam planning on adding more stuff doing pretty much the same as coins so, I would need the script to activate on trigger with ANY object. Not just coins. Thank you, btw.

Read the documentation on gameObject.tags. All of your issues you have described above will be solved when you learn that.

thank you, looks like I've got it. (just added "collision" to that line instead of "coin")