Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class EnemyManager : MonoBehaviour
- {
- private AnimManager enemyAnim;
- private Transform playerTar;
- public Rigidbody myRB;
- public float speed = 5f;
- public float attackDis = 1f, chasDisAfterAttack = 1f;
- public float defultAttackTime = 2f, currentAttackTime;
- public bool followPlayer, attackPlayer;
- private void Awake()
- {
- enemyAnim = GetComponentInChildren<AnimManager>();
- myRB = GetComponent<Rigidbody>();
- playerTar = GameObject.FindGameObjectWithTag("Player").transform;
- }
- private void Start()
- {
- followPlayer = true;
- currentAttackTime = defultAttackTime;
- }
- private void Update()
- {
- AttackPlayer();
- }
- private void FixedUpdate()
- {
- FollowTarget();
- }
- void FollowTarget()
- {
- if (!followPlayer)
- return;
- if(Vector3.Distance(transform.position, playerTar.position) > attackDis)
- {
- transform.LookAt(playerTar);
- myRB.velocity = transform.forward * speed;
- if(myRB.velocity.sqrMagnitude != 0)
- {
- enemyAnim.Run(true);
- }
- } else if(Vector3.Distance(transform.position,playerTar.position) <= attackDis)
- {
- myRB.velocity = Vector3.zero;
- enemyAnim.Run(false);
- followPlayer = false;
- attackPlayer = true;
- }
- }
- void AttackPlayer()
- {
- if (!attackPlayer)
- return;
- currentAttackTime += Time.deltaTime;
- if(currentAttackTime > defultAttackTime)
- {
- enemyAnim.EnemyAttack(Random.Range(0, 3));
- currentAttackTime = 0f;
- }
- if(Vector3.Distance(transform.position,playerTar.position) > attackDis + chasDisAfterAttack)
- {
- attackPlayer = false;
- followPlayer = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement