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);
}
}
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?