2D Spine Animation not playing
My question is simple but I cant find any answers or figure out what I am doing wrong.
When I shoot at the enemy it doesn't play the hurt animation (or any other animation then before).
I have 3 scripts for this, one for the animation, one for the enemy and one for the projectile.
The EnemyController have the Damage function:
public void DamageEnemy(int damage)
{
currentHealth -= damage;
animationController.SetCharacterState("Hurt");
if (currentHealth <= 0)
{
currentHealth = 0;
animationController.SetCharacterState("Die");
Destroy(gameObject, deathAnimationTime);
}
}
And the PlayerProjectile script have an OnTriggerEnter function to run when colliding with enemy:
public int damage = 10;
public float speed = 6f;
public float lifetime = 3f;
public Vector2 direction = new Vector2(1f, 0f);
Rigidbody2D RB;
EnemyController enemy;
void Start()
{
RB = GetComponent<Rigidbody2D>();
enemy = GameObject.FindWithTag("Enemy").GetComponent<EnemyController>();
Destroy(gameObject, lifetime);
}
void Update()
{
RB.velocity = direction * speed;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Enemy"))
{
enemy.DamageEnemy(damage);
}
Destroy(gameObject);
}
and the AnimatorController is a more generall script to work for all spine animations but it looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
public class AnimationController : MonoBehaviour
{
public SkeletonAnimation skeletonAnimation;
public AnimationReferenceAsset idle, walk, jump, doubleJump, attack, hurt, die;
public string currentState;
public float currentStateTime;
public string previousState;
public float previousStateTime;
public string currentAnimation;
void Start()
{
currentState = "Idle";
SetCharacterState(currentState);
}
public void SetAnimation(AnimationReferenceAsset animation, bool loop, float timeScale)
{
if (animation.name == currentAnimation)
{
return;
}
Spine.TrackEntry animationEntry = skeletonAnimation.state.SetAnimation(0, animation, loop);
animationEntry.TimeScale = timeScale;
animationEntry.Complete += AnimationEntry_Complete;
currentAnimation = animation.name;
}
void AnimationEntry_Complete(Spine.TrackEntry trackEntry)
{
if (currentState.Equals("Jump") || currentState.Equals("Attack") || currentState.Equals("Hurt"))
{
SetCharacterState(previousState);
}
}
public void SetCharacterState(string state)
{
if (state.Equals("Walk"))
{
SetAnimation(walk, true, 1f);
}
else if (state.Equals("Jump"))
{
SetAnimation(jump, false, 1f);
}
else if (state.Equals("Attack"))
{
SetAnimation(attack, false, 2f);
}
else if (state.Equals("Hurt"))
{
SetAnimation(hurt, false, 1f);
}
else if (state.Equals("Die"))
{
SetAnimation(die, false, 1f);
}
else
{
SetAnimation(idle, true, 1f);
}
currentState = state;
}
}
If I melee hit the enemy from the PlayerController script it does the animation but not when the projectile hits so it should not be any problem with the AnimationController script (the only thing I didnt write entirely myself).
Your answer
Follow this Question
Related Questions
HELP,Watched tutorial https://www.youtube.com/watch?v=Xnyb2f6Qqzg 1 Answer
How can i stop my shooting animation after playing? 0 Answers
Can't seem to get my animations to work proper 1 Answer
How to immediately CHANGE SPEED from spotted position to another?) 1 Answer
Player should turn in the direction the player is running. (2D Game) 0 Answers