- Home /
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.
Answer by AlucardJay · Mar 22, 2013 at 02:29 PM
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 :
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.Impulse.html
there is just one slight error, it moves the main object ins$$anonymous$$d of the coins :D. How do I swap them?
use coin.rigidbody.AddForce( transform.up * power, Force$$anonymous$$ode.Impulse );
well that was just an example. I see you want to affect the coin? :
coin.rigidbody.AddForce( transform.up * power, Force$$anonymous$$ode.Impulse );
or
coin.rigidbody.AddRelativeForce( Vector3.up * power, Force$$anonymous$$ode.Impulse );
or
coin.rigidbody.AddRelativeForce( 0, power, 0, Force$$anonymous$$ode.Impulse );
Please read the code and see what it is doing.
// coin : the cached gameObject
// rigidbody : the component on that gameObject
// AddForce and AddRelativeForce : check the Unity Scripting Reference
// transform.up : the up vector in relation to the transform
// * power : the modifier of the vector
// Force$$anonymous$$ode.Impulse : This Is The Important Part, as detailed in my answer
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.
Your answer
Follow this Question
Related Questions
Adding Force to a Rigidbody through OnTriggerEnter 1 Answer
Rigidbody.Addforce not working in Unity 5.4.1 3 Answers
Creating a teleportation gun 1 Answer
OnTriggerEnter AddForce 1 Answer