- Home /
Character Hit Animation play endlessly
hello guys, i want to play the hurt animation when my character hit collider, the animation start but my character Hit Animation play endlessly. I dont know how to make him back to previous state of animation, here my code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class PlayerControl : MonoBehaviour { private Vector2 Targetpos; public float Yincrement; public float speed; public float maxHeight; public float minHeight; public int health ; public GameObject effect; private Animator anim;
// Use this for initialization
private void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
if (health <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
transform.position = Vector2.MoveTowards(transform.position, Targetpos, speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.UpArrow)&&transform.position.y<maxHeight)
{
Instantiate(effect, transform.position, Quaternion.identity);
Targetpos = new Vector2(transform.position.x,transform.position.y + Yincrement);
}
if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight)
{
Instantiate(effect, transform.position, Quaternion.identity);
Targetpos = new Vector2(transform.position.x, transform.position.y - Yincrement);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Enemy"))
{
anim.SetBool("IsHurt",true);
}
}
IEnumerator Delay()
{
yield return new WaitForSeconds(1.0f);
anim.SetBool("IsHurt", false);
}
}
Answer by teja6595 · Sep 17, 2018 at 07:05 AM
You need to disable the loop of that hurt Animation. Just click on the animation clip in the project and u will find some setting on the inspector in that uncheck the loop.
Thanks, i disable the animation loop but this time my character freeze at the end of the hurt animation, it can't go back to the previous state which as walking, can you tell me what i did wrong ._.
ok i solve the problem, i chang Setbool to SetTrigger and it works perfectly. thank you alot
Answer by myzzie · Sep 17, 2018 at 06:38 AM
Turn off animation loop.
Thanks, i disable the animation loop but this time my character freeze at the end of the hurt animation, it can't go back to the previous state which as walking, can you tell me what i did wrong ._.
Sounds like you don't have a path from 'hurt' to 'walking'. You should use AnyState to transfer your animation states, it really helps to avoid cases like these
ok i solve the problem, i chang Setbool to SetTrigger and it works perfectly. thank you alot