Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ragdoll Difficulty

Discussion in 'Editor & General Support' started by elias723, Mar 29, 2006.

  1. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    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!
     
  2. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    Also, on an unrelated topic, how do I make something "sticky" ? (sticks to surface it hits)
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  4. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    No - but it has many similarities.

    Code (csharp):
    1.  
    2. /// When the projectile collides with an object it destroys itself and replaces itself with an explosion.
    3. /// When explosion game object is activated, this script is executed.
    4.  
    5. /// This script applies a force to all nearby colliders!
    6.  
    7. // Variables declared with var are visible in the inspector and can be edited there.
    8. var explosionRadius = 5.0;
    9. var explosionPower = 10.0;
    10.  
    11. function Start() {
    12.     colliders = Physics.OverlapSphere (transform.position, explosionRadius);
    13.     for (var hit in colliders) {
    14.         var ragdoll : CharacterAndRagdoll = hit.GetComponent(CharacterAndRagdoll);
    15.         if (ragdoll) {
    16.             explosionPosition = transform.position;
    17.             bodyPosition = ragdoll.transform.position;
    18.             offset = bodyPosition - explosionPosition;
    19.             rangePercentage = (explosionRadius - offset.magnitude) / explosionRadius;
    20.             rangePercentage = Mathf.Clamp01 (rangePercentage);
    21.             ragdoll.ChangeHealth(-130 * rangePercentage);
    22.         }
    23.     }
    24.  
    25.     // Apply a force to all surrounding rigid bodies.
    26.     for (var hit in colliders) {
    27.         if (hit.rigidbody)
    28.         {
    29.             // We use a coroutine here because we want to apply force not only for one frame but several!
    30.             // This gives a nicer explosion effect.
    31.             // A coroutine can always be paused and resumed by giving special yield instructions
    32.             // Coroutines don't use threads and have minimal performance overhead.
    33.             StartCoroutine("ApplyForce", hit.rigidbody);
    34.         }
    35.     }
    36. }
    37.  
    38. // Start emitting particles
    39. particleEmitter.emit = true;
    40.  
    41. // Wait for half a second      
    42. yield new WaitForSeconds (0.5);
    43.  
    44.  
    45. // Stop Emitting
    46. // The particle system is setup to autodestroy so when no particles are left the entire game object will be killed!
    47. particleEmitter.emit = false;
    48.  
    49. function ApplyForce (body : Rigidbody)
    50. {
    51.     time = 1.0;
    52.     // Apply the force smoothly over time!
    53.     // Check if the rigid body we apply force to is still alive.
    54.     // In a real game situation it might have been hit by and explosion and was destroyed.
    55.     while (time > 0.0  body)
    56.     {
    57.         explosionPosition = transform.position;
    58.         bodyPosition = body.transform.position;
    59.        
    60.         offset = bodyPosition - explosionPosition;
    61.         direction = offset.normalized;
    62.  
    63.         // It looks a lot cooler if explosions have more upwards force throwing things into the air!
    64.         direction.y += 30;
    65.         direction = direction.normalized;
    66.        
    67.         // We apply the force based on the distance.
    68.         // Percentage will be in the range 0...1
    69.         // Where 0 means no force applied. And 1 means apply full force.
    70.         rangePercentage = (explosionRadius - offset.magnitude) / explosionRadius;
    71.         rangePercentage = Mathf.Clamp01 (rangePercentage);
    72.        
    73.         power = rangePercentage * explosionPower * time;
    74.         force = direction * power;
    75.         body.AddForceAtPosition (force, explosionPosition);
    76.  
    77.         // Yields until the next fixed update frame.
    78.         // Forces always need to be applied at fixed frame rate!               
    79.         yield new WaitForFixedUpdate ();
    80.         time -= Time.deltaTime;
    81.     }
    82. }
    83.  
    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?
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The script you posted moves a player around but does certainly not apply explosion forces to surrounding rigidbodies.
     
  6. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    I'm sorry. I put up the wrong one, but I fixed it.
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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
     
  8. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    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.