- Home /
Enemy Ragdoll help
hey guys. i just made a ragdoll for an 3d model of an enemy, which has a punch animation. i also have player health and enemy health scripts and ai scripts. i want the ragdoll animation to occur when i kill an enemy (when the enemy's health bar is gone i mean). is there a way to make a script play the ragdoll after the enemy is dead without making a prefab?
here are my scripts:
Player Combat:
using UnityEngine;
using System.Collections;
public class PlayerCombat : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
else {
GetComponent("AttackAnimation");
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if(distance < 2f) {
if(direction > 0) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-11);
}
}
}
}
Enemy Combat:
using UnityEngine;
using System.Collections;
public class EnemyCombat : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
else {
animation.Play("punch");
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if(distance < 2f) {
if(direction > 0) {
PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
ph.AddjustCurrentHealth(-8);
if(ph.curHealth == 0) {
Application.LoadLevel(5);
}
}
}
} }
AI:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(AdvancedMovement))] [RequireComponent (typeof(CapsuleCollider))] public class AI : MonoBehaviour { public float baseMeleeRange = 5; public Transform target;
private Transform _myTransform;
private const float ROTATION_DAMP = .03f;
private const float FORWARD_DAMP = .9f;
void Awake() {
CapsuleCollider cc = (GetComponent<CapsuleCollider>());
if(cc == null)
Debug.LogError("There is no Capsule Collider on this mob");
else {
cc.isTrigger = true;
}
}
void Start() {
_myTransform = transform;
GameObject go = GameObject.FindGameObjectWithTag("Player");
if(go == null)
Debug.LogError("Could not locate the player");
target = go.transform;
}
void Update() {
if(target) {
Vector3 dir = (target.position - _myTransform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
float dist = Vector3.Distance(target.position, _myTransform.position);
Debug.Log(dist);
if(direction > FORWARD_DAMP && dist > baseMeleeRange) {
SendMessage("MoveMeForward" , AdvancedMovement.Forward.forward);
}
else {
SendMessage("MoveMeForward" , AdvancedMovement.Forward.none);
}
dir = (target.position - _myTransform.position).normalized;
direction = Vector3.Dot(dir, transform.right);
if(direction > ROTATION_DAMP) {
SendMessage("RotateMe" , AdvancedMovement.Turn.right);
}
else if(direction < - ROTATION_DAMP) {
SendMessage("RotateMe" , AdvancedMovement.Turn.left);
}
else {
SendMessage("RotateMe" , AdvancedMovement.Turn.none);
}
}
}
}
and Health:
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour { public int maxHealth = 100; public int curHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10 ,40, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
do u think u could help me? thanks
Answer by irrationalistic · Jun 29, 2012 at 06:04 PM
I've implemented a pretty basic system for this, but it seems to work. If you have the ragdoll applied to your guard, but an animation component attached as well, it seems that the animation component takes precedence. As long as the animation is running, ragdoll won't apply. Simply enough, when you are ready to drop the guard, just disable the animation component, as well as any other Update/CoRoutine stuff you have running on the guard.