I have this code, but it is don`t waiting in WaitForSeconds(), only
Debug.Log(“Starting hit animation”);
otherAnimator.SetBool(“isHitten”, true);
is being completed
using System.Collections;
using UnityEngine;
public class Pikes : MonoBehaviour
{
public int damage = 1;
public float speed;
public GameObject effect;
GameObject player;
Animator otherAnimator;
void Start()
{
player = GameObject.FindWithTag("Player");
otherAnimator = player.GetComponent<Animator>();
}
private void Update()
{
transform.Translate(Vector3.left * speed);
if(Input.GetKeyDown(KeyCode.R))
{
otherAnimator.SetBool("isHitten", false);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
StartCoroutine(HandleHitAnimation());
other.GetComponent<Player>().health -= damage;
Instantiate(effect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
private IEnumerator HandleHitAnimation()
{
Debug.Log("Starting hit animation");
otherAnimator.SetBool("isHitten", true);
yield return new WaitForSeconds(1);
Debug.Log("Ending hit animation");
otherAnimator.SetBool("isHitten", false);
}
}
Help to fix this and make waitforseconds work.
New contributor
Narek Sargsyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.