Question by
AmineMo0 · Apr 28 at 05:59 PM ·
navmeshstartcoroutine
Wait For Seconds
i want the enemy to wait 2 seconds before reducing the health by 1 point every 2seconds while attacking but it wait for the fist time. and then just drop down from 10 to 0 after it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ZombieAI : MonoBehaviour
{
public float stoppingDistance;
NavMeshAgent agent;
GameObject target;
Animator animator;
int Health = 10;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
target = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
float dist = Vector3.Distance(transform.position, target.transform.position);
if(Health == 0)
{
animator.SetLayerWeight(2, Mathf.Lerp(animator.GetLayerWeight(1), 0f, Time.deltaTime * 13f));
animator.SetLayerWeight(1, Mathf.Lerp(animator.GetLayerWeight(1), 1f, Time.deltaTime * 13f));
//DestroyObject(gameObject);
}
else
{
if (dist < stoppingDistance)
{
StopEnemy();
animator.SetLayerWeight(1, Mathf.Lerp(animator.GetLayerWeight(1), 1f, Time.deltaTime * 13f));
StartCoroutine(WaitAndDie());
}
else
{
GoToTarget();
animator.SetLayerWeight(1, Mathf.Lerp(animator.GetLayerWeight(1), 0f, Time.deltaTime * 13f));
}
}
}
void GoToTarget()
{
agent.isStopped = false;
agent.SetDestination(target.transform.position);
}
void StopEnemy()
{
agent.isStopped = true;
}
IEnumerator WaitAndDie()
{
Debug.Log(Health);
yield return new WaitForSeconds(3);
ReduceHealth(1);
}
void ReduceHealth(int S)
{
Health -= S;
}
}
Comment
Your answer
Follow this Question
Related Questions
Cannot Get Facebook Profile Picture Using www in Unity Facebook SDK 4 Answers
Navmesh Agent for direction but not movement? 0 Answers
Navmesh has some problems? 0 Answers
Weird Nav Mesh issue 0 Answers