Advertisement
GoodNoodle

Untitled

Jun 7th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. public class EnemyManager : MonoBehaviour
  6. {
  7.     private AnimManager enemyAnim;
  8.  
  9.     private Transform playerTar;
  10.  
  11.     public Rigidbody myRB;
  12.  
  13.     public float speed = 5f;
  14.  
  15.     public float attackDis = 1f, chasDisAfterAttack = 1f;
  16.  
  17.     public float defultAttackTime = 2f, currentAttackTime;
  18.  
  19.     public bool followPlayer, attackPlayer;
  20.  
  21.  
  22.     private void Awake()
  23.     {
  24.         enemyAnim = GetComponentInChildren<AnimManager>();
  25.         myRB = GetComponent<Rigidbody>();
  26.  
  27.         playerTar = GameObject.FindGameObjectWithTag("Player").transform;
  28.     }
  29.  
  30.     private void Start()
  31.     {
  32.         followPlayer = true;
  33.         currentAttackTime = defultAttackTime;
  34.     }
  35.  
  36.     private void Update()
  37.     {
  38.         AttackPlayer();
  39.   }
  40.  
  41.     private void FixedUpdate()
  42.     {
  43.        
  44.         FollowTarget();
  45.     }
  46.  
  47.     void FollowTarget()
  48.     {
  49.         if (!followPlayer)
  50.             return;
  51.  
  52.         if(Vector3.Distance(transform.position, playerTar.position) > attackDis)
  53.         {
  54.             transform.LookAt(playerTar);
  55.             myRB.velocity = transform.forward * speed;
  56.  
  57.             if(myRB.velocity.sqrMagnitude != 0)
  58.             {
  59.                 enemyAnim.Run(true);
  60.                
  61.             }
  62.         } else if(Vector3.Distance(transform.position,playerTar.position) <= attackDis)
  63.         {
  64.             myRB.velocity = Vector3.zero;
  65.             enemyAnim.Run(false);
  66.  
  67.             followPlayer = false;
  68.             attackPlayer = true;
  69.         }
  70.     }
  71.  
  72.     void AttackPlayer()
  73.     {
  74.         if (!attackPlayer)
  75.             return;
  76.  
  77.         currentAttackTime += Time.deltaTime;
  78.  
  79.         if(currentAttackTime > defultAttackTime)
  80.         {
  81.             enemyAnim.EnemyAttack(Random.Range(0, 3));
  82.  
  83.             currentAttackTime = 0f;
  84.         }
  85.  
  86.         if(Vector3.Distance(transform.position,playerTar.position) > attackDis + chasDisAfterAttack)
  87.         {
  88.             attackPlayer = false;
  89.             followPlayer = true;
  90.         }
  91.     }
  92. }
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement