- Home /
Add force which throws back an object
Hello,
I would like to do the following: When my marble rolls to a mine, then the mine would explode, and throw the marble back to the opposite direction. However, I dont know how to apply the throw back force to the marble. I have tried with AddExplosionForce, but that only throws the marble up in the y axis. However, I would like it to throw back on the x and z axis when the marble rolls on the mine, and only throw the marble up, if the marble falls to the mine.
How should I do it?
I have tried with power = 25, radius = 3, and upwards$$anonymous$$odifier with both 3.0 and 0.0
Answer by Blankzz · Aug 10, 2011 at 01:13 AM
Try getting your force vector from Rigidbody.velocity. Normalize it, invert it and multiply it by an explosion strength factor. Use forcemode.acceleration so you don't have to take the mass of the marble into consideration.
public var explosionStrength : float = 10.0f;
function OnTriggerEnter (target_ : Collider)
{
var forceVec : Vector3 = -target_.rigidbody.velocity.normalized * explosionStrength;
target_.rigidbody.AddForce(forceVec,ForceMode.Acceleration);
}
Thanks, this is working fine :) Thanks for the other replies aswell.
Thanks for this implementation, it worked for me as expected.
Curiously this worked for me only when I changed from Force$$anonymous$$ode.Acceleration to Force$$anonymous$$ode.Impulse (taken from the other answer below).
Answer by MPink · Aug 09, 2011 at 04:40 PM
Thats very strange. Cant say iv used AddExplosionForce yet myself but i can see the problem your having. I would expect the 2 Force functions below to do much the same thing but they dont.
public var force : float = 10.0f;
function OnTriggerEnter (target_ : Collider) { var vec : Vector3 = target_.transform.position; vec -= this.transform.position; vec.Normalize(); vec *= this.force; target_.rigidbody.AddForce(vec,ForceMode.Impulse); // target_.rigidbody.AddExplosionForce(force,target_.transform.position,10,0,ForceMode.Impulse); }
Hope they helps you even if it dose not explain explain how to use ExplosionForce correctly.
I have copied the following code to the $$anonymous$$e, however, nothing is happening. The trigger is registered, but nothing else happens. If I remove Force$$anonymous$$ode.Impulse, it works, but sometimes it launch the ball to a different way than it should.
Answer by JUnityer · Aug 09, 2011 at 04:48 PM
Hey,
I don't know how to do that kind of effect myself, but I suggest checking out the Detonator package. You may have already seen / tried this somewhere, but maybe you could use parts from this somehow. There is a "shockwawe" component inside that makes the objects around the explosion fly away, so take a look at it.
You could make the mines with this one easily. But if you want to make it 100% yourself, well, go ahead and do it! : ) I highly appreciate that kind of work, but just wanted to suggest you an easy way.
I have tried it, however these explosions are launching the marble up aswell.
Your answer
![](https://koobas.hobune.stream/wayback/20220612173517im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Prevent Rigidbody from Climbing Steep Slopes using Forces (C#) 0 Answers
Objects continue to have force applied after escaping force range 1 Answer
How to fix/clamp rigidbody dir vector, despite Force dir 1 Answer
How to change velocity of object which move by Rigidbody. 1 Answer
Movement Physics: Max Speed and Momentun 0 Answers