Hey guys! I have been trying to make a FPS horror game. I have a bunch of animations set up on the enemy character. It plays all of them like it’s supposed to except for the animation for when the enemy is damaged.
I can’t figure out what I did wrong, or where to go from here.
using UnityEngine;
using System.Collections;
public class Enemy_Health : MonoBehaviour
{
public int maxHealth = 100;
private int currentHealth;
private Animator animator;
// Use this for initialization
void Start ()
{
currentHealth = maxHealth;
animator = GetComponent<Animator>();
}
// Update is called once per frame
public void TakeDamage(int _damage)
{
currentHealth -= _damage;
animator.SetTrigger("isHit");
if(currentHealth <= 0)
{
//call die method
Die();
}
}
void Die()
{
//Kill the enemy
//play an animation
//instantiate a ragdoll
//destroy the enemy GO
animator.SetTrigger("Dead");
}
}
Any help or suggestions is greatly appreciated!
Thanks!
It looks like you are doing everything ok.
When you call take damage you deduct from currentHealth amount of damage and set trigger on animator isHit.
From the information you posted I would guess something is wrong with your animator settings or perhaps it is in another state and while isHit trigger is being set it is never executed because it is not in state that it needs to be.
Must be in your animator, try connecting your hit state to anyState so it will fire regardless of what state you are currently in. Also be sure to turn off exit time on the transition
I always copy this code over from project to project when I work with “Legacy” animations. This script does not use the “Animator” it uses the “Animation” component with “Legacy” animations.
using UnityEngine;
using System.Collections;
public class AnimationControl : MonoBehaviour {
public Animation characterAnimation;
public string currentAnimation = "NA";
public string[] animationNames;
public bool playingAnimation = false;
public float animationTime = 0;
public float animationLength = 0;
string animation_name = "";
public int state = 0;
public int previousState = -1;
public float speed = 1f;
public float blend = 0.1f;
bool initialized = false;
public bool clamp = false;
void Awake(){
initialize();
}
void Update (){
if (playingAnimation){
animationTime+= Time.deltaTime;
if (animationTime >= animationLength){
if (!clamp){
animationTime = 0;
playingAnimation = false;
currentAnimation = "NA";
}
else{
characterAnimation[currentAnimation].time = animationLength;
}
}
}
animation_name = animationNames[state];
characterAnimation[animation_name].wrapMode = WrapMode.Loop;
characterAnimation[animation_name].layer = 0;
characterAnimation[animation_name].speed = speed;
characterAnimation.CrossFade(animation_name, blend);
}
public void PlayAnim(string name, float customSpeed, float blendChosen){
characterAnimation.Stop();
characterAnimation[name].layer = 1;
characterAnimation[name].speed = customSpeed;
if (clamp){
characterAnimation[name].wrapMode = WrapMode.ClampForever;
}
else{
characterAnimation[name].wrapMode = WrapMode.Once;
}
animationTime = 0;
animationLength = characterAnimation[name].length/customSpeed;
currentAnimation = name;
characterAnimation.CrossFade(name,blendChosen);
playingAnimation = true;
}
void initialize(){
//characterAnimation = GetComponent<Animation>();
int num = characterAnimation.GetClipCount();
animationNames = new string[num];
int i = 0;
foreach(AnimationState stateAnimation in characterAnimation){
animationNames.SetValue(stateAnimation.name, i);
i++;
}
initialized = true;
}
}
basically how it works is, if you want your character to be in the idle state, just change your “state” variable to the integer index of the animation in the “Animation” component as listed. running state set to 1 for running, state 2 for swimming, and for attacking you use the following code.
animationControl.PlayAnim("Attack 1", 1f, 0.1f);
first variable is the animation name as a string, second argument is the animation playback speed, and the third is the blend amount, I always use 0.1f