• Unity
  • Hit animation does not play

Related Discussions
...

Hit animation does not play

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;

public class Player : MonoBehaviour
{
    public SkeletonAnimation skeletonAnimation;
    public AnimationReferenceAsset idle, walk, jump, hit;
    public string currentState;
    public float speed;
    public float movement;
    public Rigidbody2D rigidbody;
    public Joystick joystick;
    public string currentAnimation;
    public float jumpSpeed;
    public string previousState;
    
// Start is called before the first frame update void Start() { rigidbody = GetComponent<Rigidbody2D>(); currentState = "Idle"; SetCharacterState(currentState); } // Update is called once per frame void Update() { Move(); } public void SetAnimation(AnimationReferenceAsset animation, bool loop, float timeScale) { if(animation.name.Equals(currentAnimation)){ return; } Spine.TrackEntry animationEntry = skeletonAnimation.state.SetAnimation(0, animation, loop); animationEntry.TimeScale = timeScale; animationEntry.Complete += AnimationEntry_Complete; currentAnimation = animation.name; } private void AnimationEntry_Complete(Spine.TrackEntry trackEntry){ Debug.Log("AnimationEntry_Complete"); if(currentState.Equals("jump") || currentState.Equals("hit") ){ SetCharacterState(previousState); Debug.Log("complete"); } } public void SetCharacterState(string state){ if (state.Equals("walk")) { SetAnimation(walk, true, 2f); } else if (state.Equals("hit")) { SetAnimation(hit, false, 1f); } else if (state.Equals("jump")) { SetAnimation(jump, false, 1f); } else { SetAnimation(idle, true, 1f); } currentState = state; } public void Move() { movement = joystick.Horizontal; rigidbody.velocity = new Vector2(movement * speed, rigidbody.velocity.y); if(movement != 0) { if (!currentState.Equals("jump")){ SetCharacterState("walk"); } if (!currentState.Equals("hit")){ SetCharacterState("walk"); } if (movement > 0) { transform.localRotation = Quaternion.Euler(0, 0, 0); } else { transform.localRotation = Quaternion.Euler(0, 180, 0); } } else { if (!currentState.Equals("jump")){ SetCharacterState("Idle"); } if (!currentState.Equals("hit")){ SetCharacterState("Idle"); } } } public void Jump() { rigidbody.velocity = new Vector2(rigidbody.velocity.x, jumpSpeed); if (!currentState.Equals("jump")) { previousState = currentState; } SetCharacterState("jump"); } public void Hit() { if (!currentState.Equals("hit")) { previousState = currentState; } SetCharacterState("hit"); } }

As far as I have tried your code, I think the following part in the Move() method is the cause:

else {
    if (!currentState.Equals("jump")){
        SetCharacterState("Idle");
    } 
    if (!currentState.Equals("hit")){
        SetCharacterState("Idle");
    } 
}

It is written as "If currentState is not jump, set the character state to Idle", so even if the current state is hit, it is forced to be Idle. I think the following code should work:

else {
    if(!currentState.Equals("jump") && !currentState.Equals("hit")){
        SetCharacterState("Idle");
    }
}

For sake of completeness: a similar logic problem can be found a few lines above:

 if (!currentState.Equals("jump")){
    SetCharacterState("walk");
}
if (!currentState.Equals("hit")){
    SetCharacterState("walk");
}