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?
What values are you using when you call the function?
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.
I want to send a int that the script can then use to get a specific item from a data object The int is part of a randomized array so I sadly can't just have the int (or the info from the data object) be stored directly in the other script
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 mine, however, nothing is happening. The trigger is registered, but nothing else happens. If I remove ForceMode.Impulse, it works, but sometimes it launch the ball to a different way than it should.
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.
What values are you using when you call the function?
– tnetennbaI have tried with power = 25, radius = 3, and upwardsModifier with both 3.0 and 0.0
– DreekaAnd what about the position of the explosive force?
– tnetennbaI use the position of the mine
– Dreeka