public class Zombie : MonoBehaviour
{
public int hp;
private NavMeshAgent agent;
private GameObject player;
private Animator anim;
private int modify;
private bool runner;
private bool stop;
private bool killed;
public LayerMask layermask;
public Transform hip;
public AudioSource walk;
public AudioSource death;
public AudioSource attack;
public AudioClip[] Runner;
public AudioClip[] zombie;
void Start()
{
modify = Random.Range(1, 10);
agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
player = GameObject.FindWithTag("Player");
StartCoroutine(ChangeAud());
}
IEnumerator ChangeAud()
{
while (true)
{
if (modify <= 3)
{
attack.clip = Runner[Random.Range(0, Runner.Length)];
}
else
{
attack.clip = zombie[Random.Range(0, zombie.Length)];
}
yield return new WaitForSeconds(5f);
}
}
void Update()
{
StopOrResume();
if(!killed) Attack();
if (modify <= 10)
{
runner = true;
agent.speed = 1f;
}
float distance = Vector3.Distance(transform.position, player.transform.position);
/*if (modify > 3)
{
if (distance >= 50)
{
runner = true;
agent.speed = 4f;
}
else
{
runner = false;
agent.speed = 0.5f;
}
}*/
if (hp <= 0)
{
if (agent.isOnNavMesh) stop = true;
anim.SetBool("Walk", false);
anim.SetBool("Attack", false);
anim.SetBool("Run", false);
anim.SetBool("Idle", false);
anim.SetBool("Death", true);
this.gameObject.GetComponent<CapsuleCollider>().enabled = false;
this.gameObject.GetComponent<Rigidbody>().isKinematic = true;
this.gameObject.GetComponent<NavMeshAgent>().enabled = false;
walk.Stop();
attack.Stop();
if (!killed)
{
death.Play();
Spawner.spawned--;
killed = true;
}
Destroy(this.gameObject, 40f);
}
if (hp > 0)
{
agent.destination = player.transform.position;
if (!stop && runner)
{
if(!walk.isPlaying) walk.Play();
anim.SetBool("Walk", false);
anim.SetBool("Run", true);
}
else
{
walk.Stop();
anim.SetBool("Walk", false);
anim.SetBool("Run", false);
}
}
}
void Attack()
{
RaycastHit hit;
if (Physics.Raycast(hip.transform.position, -hip.transform.up, out hit, 0.9f, layermask))
{
Debug.Log(hit.collider.tag);
if (hit.collider.gameObject.TryGetComponent(out Player player))
{
if (agent.isOnNavMesh) stop = true;
anim.SetBool("Run", false);
anim.SetBool("Idle", false);
anim.SetBool("Walk", false);
anim.SetBool("Attack", true);
if (runner) player.TakeDamage(1);
else player.TakeDamage(1);
if (!attack.isPlaying) attack.Play();
}
if (hit.collider.gameObject.TryGetComponent(out Building building))
{
if (agent.isOnNavMesh) stop = true;
anim.SetBool("Run", false);
anim.SetBool("Idle", false);
anim.SetBool("Walk", false);
anim.SetBool("Attack", true);
if (runner) building.TakeDamage(60);
else building.TakeDamage(40);
if (!attack.isPlaying) attack.Play();
}
}
else
{
if (agent.isOnNavMesh) stop = false;
anim.SetBool("Attack", false);
if (attack.isPlaying) attack.Stop();
}
}
void StopOrResume()
{
if (stop && agent.isOnNavMesh) agent.Stop();
else if(agent.isOnNavMesh) agent.Resume();
}
}
if (building.hp > 0)
{
stop = true;
anim.SetBool("Run", false);
anim.SetBool("Idle", false);
anim.SetBool("Walk", false);
anim.SetBool("Attack", true);
if (runner) building.hp -= 60f * Time.deltaTime;
else building.hp -= 40f * Time.deltaTime;
}
else
{
stop = false;
Debug.Log("okkk");
anim.SetBool("Attack", false);
}
void Update()
{
if (Input.GetKey(KeyCode.E) && canvas.activeInHierarchy)
{
slider.value += 0.005f * Time.deltaTime;
}
else
{
slider.value = 0;
}
RaycastHit hit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 2f) && !Input.GetKey(KeyCode.Tab))
{
if (hit.collider.gameObject.TryGetComponent(out Building building))
{
slider.maxValue = building.slider.maxValue;
slider.value = building.slider.value;
if (building.hp != building.maxHp)
{
canvas.SetActive(true);
building.slider.value = slider.value;
}
else
{
canvas.SetActive(false);
}
}
else
{
canvas.SetActive(false);
}
}
else
{
canvas.SetActive(false);
}
}
public NavMeshAgent navMeshAgent;
public Transform player;
public Transform[] points;
[SerializeField] private int currentPoint;
void Start()
{
currentPoint = 0;
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
CheckPlayerOnRoom();
if (currentPoint >= points.Length) currentPoint = 0;
if (navMeshAgent.remainingDistance < 1f)
{
currentPoint += 1;
navMeshAgent.SetDestination(points[currentPoint].position);
}
}
void CheckPlayerOnRoom()
{
if (Check.playerOnRoom && door.transform.rotation == Quaternion.Euler(0, -90, 0))
{
if(currentPoint == 0) navMeshAgent.SetDestination(points[1].position);
}
else
{
currentPoint = 0;
navMeshAgent.SetDestination(player.transform.position);
}
}