- Home /
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);
}
}
Just use the same code you used on start, but when you destroy, add a delay Destroy(gameObject, 5);
I tried it but now it plays the animation and ins$$anonymous$$d of ragdolling it just disappears after 5 seconds.
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);
}
}
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?
'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
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
Your answer
Follow this Question
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