Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Zombie_AI : MonoBehaviour {
- private float lastTime;
- [SerializeField] private float Distance;
- public Transform Target;
- [SerializeField] private float lookAtDistance = 30f;
- [SerializeField] private float chaseRange = 20f;
- [SerializeField] private float attackRange = 2f;
- private float moveSpeed;
- [SerializeField] private float Damping = 6f;
- [SerializeField] private float TheDamage = 25f;
- public CharacterController controller;
- float gravity = 20f;
- Vector3 moveDirection = Vector3.zero;
- void Start()
- {
- FindHealth();
- lastTime = Time.time;
- }
- void Update()
- {
- Distance = Vector3.Distance(Target.position, transform.position);
- if (Distance < lookAtDistance)
- {
- LookAt();
- }
- if (Distance < attackRange)
- {
- Attack();
- if(gameObject.GetComponent<Animator>().GetBool("isAttacking")==false || //// ces deux lignes servent juste à pas repeter si c'est déjà fait
- gameObject.GetComponent<Animator>().GetBool("isChasing")==true)
- {
- gameObject.GetComponent<Animator>().SetBool("isAttacking", true);
- gameObject.GetComponent<Animator>().SetBool("isChasing", false);
- }
- }
- if (Distance < chaseRange && Distance > attackRange)
- {
- Chase();
- if (gameObject.GetComponent<Animator>().GetBool("isAttacking") == true || /// Pareil ici
- gameObject.GetComponent<Animator>().GetBool("isChasing") == false)
- {
- gameObject.GetComponent<Animator>().SetBool("isAttacking", false);
- gameObject.GetComponent<Animator>().SetBool("isChasing", true);
- }
- }
- }
- void LookAt()
- {
- var lookPos = Target.position - transform.position;
- lookPos.y = 0;
- var rotation = Quaternion.LookRotation(lookPos);
- transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
- }
- void Chase()
- {
- gameObject.GetComponent<Rigidbody>().velocity = transform.forward * moveSpeed;
- //moveDirection = transform.forward;
- //moveDirection *= moveSpeed;
- ////moveDirection.y -= gravity * Time.deltaTime;
- ////gameObject.GetComponent<Rigidbody>().AddForce(moveDirection * Time.deltaTime * moveSpeed);
- //gameObject.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * moveSpeed);
- }
- void Attack()
- {
- if (Time.time >= lastTime + 1f)
- {
- Target.SendMessage("ApplyDammage", TheDamage);
- Debug.Log("The Ennemy has attacked");
- lastTime = Time.time;
- }
- void FindHealth()
- {
- Target = GameObject.Find("PlayerStats").GetComponent<Transform>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement