Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by KillerPenguin · Dec 18, 2016 at 11:39 AM · unity 5scripting problemenemyscriptingbasicsragdoll

How to make an enemy ragdoll on death?

I'm having trouble figuring out how to get an enemy to ragdoll when hp drops below zero. I have a basic code that enables ragdolls when scene starts but I need it enabled after enemy is killed but while they are alive they are rigid. Any help appreciated, thanks!

Side note: fairly new to unity and coding.

 public int maxHp = 10;
 private int hp;
 Component[] bodyparts;
 
 void Start()
 {
     hp = maxHp;
     bodyparts = GetComponentsInChildren<Rigidbody>();
 }

 public void Damage(DamageInfo info)
 {
     if (hp <= 0) return;
     hp -= info.damage;
     if (hp <= 0) Die();
 }

 void Die()
 {
     Destroy(gameObject);
 }

}

Comment
Add comment · Show 2
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
avatar image ProtoTerminator · Dec 18, 2016 at 02:33 PM 0
Share

Just use the same code you used on start, but when you destroy, add a delay Destroy(gameObject, 5);

avatar image KillerPenguin ProtoTerminator · Dec 19, 2016 at 12:10 AM 0
Share

I tried it but now it plays the animation and ins$$anonymous$$d of ragdolling it just disappears after 5 seconds.

1 Reply

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

Answer by KillerPenguin · Dec 19, 2016 at 10:57 AM

Managed to fix it after looking around on the web a little more I added code in order to disable the animator. Here's the new code in case anyone else has the same troubles.

using UnityEngine; using System.Collections;

public class ExampleEnemy : MonoBehaviour {

 public int maxHp = 10;
 private int hp;

 void SetKinematic(bool newValue)
 {
     Rigidbody[] bodies = GetComponentsInChildren<Rigidbody>();
     foreach (Rigidbody rb in bodies)
     {
         rb.isKinematic = newValue;
     }
 }

 void Start()
 {
     SetKinematic(true);
     hp = maxHp;
 }

 public void Damage(DamageInfo info)
 {
     if (hp <= 0) return;
     hp -= info.damage;
     if (hp <= 0) Die();
 }

 void Die()
 {
     SetKinematic(false);
     GetComponent<Animator>().enabled = false;
     Destroy(gameObject, 5);
 }

}

Comment
Add comment · Show 3 · 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
avatar image Klipsgoboomyt · Mar 18, 2021 at 06:33 PM 0
Share

Im getting an error with public void Damage(DamageInfo info) { if (hp <= 0) return; hp -= info.damage; if (hp <= 0) Die(); } The Damage(DamageInfo info) part is not working I have this error The type or namespace name 'DamageInfo' could not be found (are you missing a using directive or an assembly reference?)

Can someone please help?

avatar image Llama_w_2Ls Klipsgoboomyt · Mar 18, 2021 at 07:02 PM 0
Share

'DamageInfo' is a custom object that KillerPenguin created that contains information on what damage the player or enemy took. It might look something like this:

 class DamageInfo
 {
      public int damage;
 
      public DamageInfo(int _damage)
      {
           damage = _damage;
      }
 }



If you don't want to use an object, for simplicity's sake, just change the parameter to an int containing the amount of damage you want to apply. For example:

  public void Damage(int damage)
  {
      if (hp <= 0) return;
      hp -= damage;
      if (hp <= 0) Die();
  }


Hope that helps @Klipsgoboomyt

avatar image Klipsgoboomyt Llama_w_2Ls · Mar 18, 2021 at 07:52 PM 0
Share

Yes that helps, but where would I locate the class DamageInfo { public int damage;

   public DamageInfo(int _damage)
   {
        damage = _damage;
   }

} ? Im new to c# but I use a lot of html and js

@Llama_w_2Ls

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

8 People are following this question.

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

Related Questions

Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 Answers

Point Counter Works Only Once! 1 Answer

Referencing a Cs Script from a JS within the Compile Order 0 Answers

Adding callbacks/events to an instantiated prefab 2 Answers

How do I inherit certain varibles and functions from another script. 1 Answer


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