Hey OTEE. I’m using a heavily modified CharacterAndRagdoll script on the default soldier guy, and now I’m playing with a rocket launcher I made which uses a moderately modified Explosion script when it collides with anything. When he dies, he turns into a ragdoll… but for some reason does not react to the rocket that just exploded next to him. I can’t figure out why - I’ve tried fiddling with the time interval over which the explosion applies a force to the things within its sphere radius, but for some reason nothing works. He will respond to new rockets after he dies, but not the first one for some reason. Next I’m gonna try delaying the explosion a split second, but that probably won’t make a difference either. Hayelp!
Also, on an unrelated topic, how do I make something “sticky” ? (sticks to surface it hits)
Are you using the script posted here?
http://forum.otee.dk//viewtopic.php?t=1382&start=0
No - but it has many similarities.
/// When the projectile collides with an object it destroys itself and replaces itself with an explosion.
/// When explosion game object is activated, this script is executed.
/// This script applies a force to all nearby colliders!
// Variables declared with var are visible in the inspector and can be edited there.
var explosionRadius = 5.0;
var explosionPower = 10.0;
function Start() {
colliders = Physics.OverlapSphere (transform.position, explosionRadius);
for (var hit in colliders) {
var ragdoll : CharacterAndRagdoll = hit.GetComponent(CharacterAndRagdoll);
if (ragdoll) {
explosionPosition = transform.position;
bodyPosition = ragdoll.transform.position;
offset = bodyPosition - explosionPosition;
rangePercentage = (explosionRadius - offset.magnitude) / explosionRadius;
rangePercentage = Mathf.Clamp01 (rangePercentage);
ragdoll.ChangeHealth(-130 * rangePercentage);
}
}
// Apply a force to all surrounding rigid bodies.
for (var hit in colliders) {
if (hit.rigidbody)
{
// We use a coroutine here because we want to apply force not only for one frame but several!
// This gives a nicer explosion effect.
// A coroutine can always be paused and resumed by giving special yield instructions
// Coroutines don't use threads and have minimal performance overhead.
StartCoroutine("ApplyForce", hit.rigidbody);
}
}
}
// Start emitting particles
particleEmitter.emit = true;
// Wait for half a second
yield new WaitForSeconds (0.5);
// Stop Emitting
// The particle system is setup to autodestroy so when no particles are left the entire game object will be killed!
particleEmitter.emit = false;
function ApplyForce (body : Rigidbody)
{
time = 1.0;
// Apply the force smoothly over time!
// Check if the rigid body we apply force to is still alive.
// In a real game situation it might have been hit by and explosion and was destroyed.
while (time > 0.0 body)
{
explosionPosition = transform.position;
bodyPosition = body.transform.position;
offset = bodyPosition - explosionPosition;
direction = offset.normalized;
// It looks a lot cooler if explosions have more upwards force throwing things into the air!
direction.y += 30;
direction = direction.normalized;
// We apply the force based on the distance.
// Percentage will be in the range 0...1
// Where 0 means no force applied. And 1 means apply full force.
rangePercentage = (explosionRadius - offset.magnitude) / explosionRadius;
rangePercentage = Mathf.Clamp01 (rangePercentage);
power = rangePercentage * explosionPower * time;
force = direction * power;
body.AddForceAtPosition (force, explosionPosition);
// Yields until the next fixed update frame.
// Forces always need to be applied at fixed frame rate!
yield new WaitForFixedUpdate ();
time -= Time.deltaTime;
}
}
I also have a question about using the rays you told me about with the CharacterAndRagdoll.js script. If I want the ray to affect the health of the character (regular soldier) it hits through its CharacterAndRagdoll script, how would I get to the CharARag script if I don’t know what body part I’m hitting?
The script you posted moves a player around but does certainly not apply explosion forces to surrounding rigidbodies.
I’m sorry. I put up the wrong one, but I fixed it.
Your script doesn’t search for all colliders after the characters have been turned into ragdolls.
The script posted here does that and works correctly, so just use that.
http://forum.otee.dk//viewtopic.php?t=1382
Well… yes it does - it finds the ragdolls and makes them ragdolls in the first part (that is, if their health is affected enough) of the Start() function. I’ve modified it now in a new way so that now it works, though. I directly called ragdoll.SetRagdollEnabled(true) if the health detriment was big enough.
Also, I retract my other question about the rays and the C&R script.