Advertisement
kura2yamato

EnemyController

Jun 26th, 2022
952
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EnemyController : MonoBehaviour
  6. {
  7.     public float attackDistance; //minimum Distance for attack
  8.     public float moveSpeed;     //enemy move speed
  9.     public float timer; //timer for cooldown between attacks
  10.     public Transform leftLimit;  //patrol point
  11.     public Transform rightLimit;  // patrol point
  12.     public GameObject hotZone;    //alarm zone for detecting player
  13.     public GameObject triggerArea;
  14.  
  15.     public Transform groundcheck;  
  16.     private bool isGrounded;
  17.     public LayerMask groundFloor;
  18.  
  19.     public Transform target;
  20.     private Animator anim;
  21.     private float distance; //store the distance between enemy and player
  22.     private bool attackMode;
  23.     public bool inRange;//check if player is in range
  24.     private bool cooling;//checked if enemy is cooling after attack
  25.     private float intTimer;//store initial timer
  26.  
  27.     private void Awake()
  28.     {
  29.         intTimer = timer; //store the initial value of timer
  30.         anim = GetComponent<Animator>();
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36. //=======problemnya?? tidak deteksi ground ama ceknya ??============
  37.         isGrounded = Physics2D.OverlapCircle(groundcheck.position, 0.1f, groundFloor);
  38.  
  39.         if (!attackMode && isGrounded)
  40.         {
  41.             Move();
  42.         }
  43.         else if (!isGrounded)
  44.         {
  45.             Patrol();
  46.             Move();
  47.         }
  48.  
  49.         if (!InsideOfLimits() && !inRange && !anim.GetCurrentAnimatorStateInfo(0).IsName("Ghost_Attack"))
  50.         {
  51.             Patrol();
  52.         }
  53.  
  54.         if (inRange)
  55.         {
  56.             EnemyLogic();
  57.         }
  58.     }
  59.  
  60.     void EnemyLogic()
  61.     {
  62.         distance = Vector2.Distance(transform.position, target.position);
  63.  
  64.         if (distance > attackDistance)
  65.         {
  66.             StopAttack();
  67.         }
  68.  
  69.         else if (attackDistance >= distance && cooling == false)
  70.         {
  71.             Attack();
  72.         }
  73.  
  74.         if (cooling)
  75.         {
  76.             cooldown();
  77.             anim.SetBool("Attack", false);
  78.         }
  79.     }
  80.  
  81.     void Move()
  82.     {
  83.         anim.SetBool("Walk", true);
  84.         if (!anim.GetCurrentAnimatorStateInfo(0).IsName("Ghost_Attack"))
  85.         {
  86.             Vector2 targetPosition = new Vector2(target.position.x, transform.position.y);
  87.             transform.position = Vector2.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
  88.         }
  89.     }
  90.  
  91.     void Attack()
  92.     {
  93.         isGrounded = true;
  94.         timer = intTimer; //reset timer when player enter attack range
  95.         attackMode = true;//to check if enemy can still attack or not
  96.  
  97.         anim.SetBool("Walk", false);
  98.         anim.SetBool("Attack", true);
  99.     }
  100.  
  101.     void cooldown()
  102.     {
  103.         timer -= Time.deltaTime;
  104.         if (timer <= 0 && cooling && attackMode)
  105.         {
  106.             cooling = false;
  107.             timer = intTimer;
  108.         }
  109.     }
  110.  
  111.     void StopAttack()
  112.     {
  113.         cooling = false;
  114.         attackMode = false;
  115.         anim.SetBool("Attack", false);
  116.     }
  117.     public void TriggerCooling()
  118.     {
  119.         cooling = true;
  120.     }
  121.  
  122.     private bool InsideOfLimits()
  123.     {
  124.         return transform.position.x > leftLimit.position.x && transform.position.x < rightLimit.position.x;
  125.     }
  126.  
  127.     public void Patrol()
  128.     {
  129.         float distanceToLeft = Vector2.Distance(transform.position, leftLimit.position);
  130.         float distanceToRight = Vector2.Distance(transform.position, rightLimit.position);
  131.  
  132.         if (distanceToLeft > distanceToRight)
  133.         {
  134.             target = leftLimit;
  135.         }
  136.         else
  137.         {
  138.             target = rightLimit;
  139.         }
  140.  
  141.         Flip();
  142.     }
  143.  
  144.     public void Flip()
  145.     {
  146.         Vector3 rotation = transform.eulerAngles;
  147.         //if (transform.position.x > target.position.x)
  148.         if (transform.position.x < target.position.x)
  149.         {
  150.             rotation.y = 180f;
  151.         }
  152.         else
  153.         {
  154.             rotation.y = 0f;
  155.         }
  156.  
  157.         transform.eulerAngles = rotation;
  158.     }
  159. }
  160.  
Advertisement
RAW Paste Data Copied
Advertisement