Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Doneyes · Sep 08, 2013 at 09:56 PM · rigidbodyforcebulletdeathhealth

How do add force from bullet on death?

Too confusing to explain in the title:

On the final shot to the body when the enemies health = 0, it turns into a ragdoll. I want the bullet prefab to give the ragdoll a push when it hits and the player is dead so that it looks like the player died from the bullet.

So if the gun shoots the enemy point blank in the face, the head will fly backwards bringing the rest of the body with it. How is this done?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Sep 09, 2013 at 01:48 AM

This depends a lot on how your character is organized, how you're applying damage to it, how you're replacing it with the ragdoll etc. Based on the old-and-good-but-no-more-available FPS Tutorial, the easiest way to implement this is to apply forces to the object hit with AddExplosionForce: this distributes the forces among the ragdoll rigidbodies according to the distance, thus points nearest to the impact point will be more affected. The script below was adapted from the above mentioned FPS Tutorial (ExplosionAdvanced.js) and produced very reasonable results - attach it to the bullet:

 var impactRadius: float = 3.0;
 var impactPower: float = 50.0;
 var damage: float = 10.0;
 
 function OnCollisionEnter(coll: Collision){
     var hit: ContactPoint = coll.contacts[0]; // get contact info
     // apply damage to the object hit
     coll.transform.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
     // if killed, the enemy now has been replaced by a ragdoll - get its colliders in an array:
     var colliders: Collider[] = Physics.OverlapSphere(hit.point, impactRadius);
     // check if there are rigidbodies among them...
     for (var col: Collider in colliders){
         var rigid: Rigidbody = col.rigidbody;
         if (rigid && !rigid.isKinematic){ // if it's a rigidbody...
             // apply a force inversely proportional to the distance from the hit point:
             rigid.AddExplosionForce(impactPower, hit.point, impactRadius);
         }
     }
     // destroy the bullet
     Destroy (gameObject);
 }

This script expects that the enemy has a function ApplyDamage, which will hurt it and, when killed, replace it with a ragdoll. If you're basing your scripts in the FPS Tutorial, the enemies probably have the CharacterDamage.js script. If not, that's the CharacterDamage script:

 var hitPoints = 100.0;
 var deadReplacement : Transform;
 var dieSound : AudioClip;
 
 function ApplyDamage (damage : float) {
     // We already have less than 0 hitpoints, maybe we got killed already?
     if (hitPoints <= 0.0)
         return;
 
     hitPoints -= damage;
     if (hitPoints <= 0.0)
     {
         Detonate();
     }
 }
 
 function Detonate () {
     // Destroy ourselves
     Destroy(gameObject);
     
     // Play a dying audio clip
     if (dieSound)
         AudioSource.PlayClipAtPoint(dieSound, transform.position);
 
     // Replace ourselves with the dead body
     if (deadReplacement) {
         var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
         
         // Copy position & rotation from the old hierarchy into the dead replacement
         CopyTransformsRecurse(transform, dead);
     }
 }
 
 static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
     dst.position = src.position;
     dst.rotation = src.rotation;
     
     for (var child : Transform in dst) {
         // Match the transform with the same name
         var curSrc = src.Find(child.name);
         if (curSrc)
             CopyTransformsRecurse(curSrc, child);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access component that the raycast hit 1 Answer

Adding force to a bullet as it hits object? 2 Answers

AddExplosionForce 0 Answers

How can i set AddRelativeForce relative to how fast/slow im moving my mouse? 2 Answers

Add force which throws back an object 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges