Advertisement
Guest User

Zombie_AI

a guest
Nov 9th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Zombie_AI : MonoBehaviour {
  6.  
  7.     private float lastTime;
  8.     [SerializeField] private float Distance;
  9.     public Transform Target;
  10.     [SerializeField] private float lookAtDistance = 30f;
  11.     [SerializeField] private float chaseRange = 20f;
  12.     [SerializeField] private float attackRange = 2f;
  13.     private float moveSpeed;
  14.     [SerializeField] private float Damping = 6f;
  15.     [SerializeField] private float TheDamage = 25f;
  16.     public CharacterController controller;
  17.     float gravity = 20f;
  18.     Vector3 moveDirection = Vector3.zero;
  19.     void  Start()
  20.     {      
  21.         FindHealth();
  22.         lastTime = Time.time;
  23.     }
  24.     void Update()
  25.     {
  26.         Distance = Vector3.Distance(Target.position, transform.position);
  27.  
  28.         if (Distance < lookAtDistance)
  29.         {
  30.             LookAt();
  31.         }
  32.  
  33.         if (Distance < attackRange)
  34.         {
  35.             Attack();
  36.            if(gameObject.GetComponent<Animator>().GetBool("isAttacking")==false || //// ces deux lignes servent juste à pas repeter si c'est déjà fait
  37.            gameObject.GetComponent<Animator>().GetBool("isChasing")==true)
  38.               {                
  39.                 gameObject.GetComponent<Animator>().SetBool("isAttacking", true);
  40.                 gameObject.GetComponent<Animator>().SetBool("isChasing", false);
  41.              }                    
  42.         }
  43.        
  44.         if (Distance < chaseRange && Distance > attackRange)
  45.         {
  46.             Chase();
  47.              if (gameObject.GetComponent<Animator>().GetBool("isAttacking") == true || /// Pareil ici
  48.              gameObject.GetComponent<Animator>().GetBool("isChasing") == false)
  49.                {                
  50.                 gameObject.GetComponent<Animator>().SetBool("isAttacking", false);
  51.                 gameObject.GetComponent<Animator>().SetBool("isChasing", true);
  52.                }
  53.         }
  54.     }
  55.     void LookAt()
  56.         {
  57.             var lookPos = Target.position - transform.position;
  58.             lookPos.y = 0;
  59.             var rotation = Quaternion.LookRotation(lookPos);
  60.             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
  61.         }
  62.     void Chase()
  63.     {
  64.         gameObject.GetComponent<Rigidbody>().velocity = transform.forward * moveSpeed;
  65.         //moveDirection = transform.forward;
  66.         //moveDirection *= moveSpeed;
  67.         ////moveDirection.y -= gravity * Time.deltaTime;
  68.         ////gameObject.GetComponent<Rigidbody>().AddForce(moveDirection * Time.deltaTime * moveSpeed);
  69.         //gameObject.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * moveSpeed);
  70.     }
  71.     void Attack()
  72.     {
  73.         if (Time.time >= lastTime + 1f)
  74.         {
  75.         Target.SendMessage("ApplyDammage", TheDamage);
  76.         Debug.Log("The Ennemy has attacked");
  77.             lastTime = Time.time;
  78.     }
  79.     void FindHealth()
  80.     {
  81.         Target = GameObject.Find("PlayerStats").GetComponent<Transform>();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement