108 lines
2.6 KiB
C#
108 lines
2.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class FollowingEnemy : Enemy
|
|
{
|
|
public Transform target;
|
|
public float chaseRadius;
|
|
public float attackRadius;
|
|
public Transform homePosition;
|
|
|
|
public float roundingDistance;
|
|
private Rigidbody2D myRigidbody;
|
|
public Animator anim;
|
|
public GameObject other;
|
|
|
|
public bool inRange = false;
|
|
public GameObject player;
|
|
private bool firstAttack = false;
|
|
|
|
private float timer = 0f;
|
|
private float waitTime = 1.0f;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
myRigidbody = GetComponent<Rigidbody2D>();
|
|
anim = GetComponent<Animator>();
|
|
target = GameObject.FindWithTag("Player").transform;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
CheckDistance();
|
|
//StartCoroutine(Timer());
|
|
|
|
//Code below is for dealing damage to player
|
|
|
|
if (inRange == true)
|
|
{
|
|
if (firstAttack == false)
|
|
{
|
|
if (timer >= 0.15f)
|
|
{
|
|
firstAttack = true;
|
|
other.GetComponent<Player>().TakeDamage(baseAttack);
|
|
timer = 0f;
|
|
}
|
|
}
|
|
if (timer >= waitTime)
|
|
{
|
|
timer = 0f;
|
|
other.GetComponent<Player>().TakeDamage(baseAttack);
|
|
}
|
|
timer += Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
|
|
void CheckDistance()
|
|
{
|
|
if(Vector3.Distance(target.position, transform.position) <= chaseRadius && Vector3.Distance(target.position, transform.position) > attackRadius)
|
|
{
|
|
transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
|
|
}
|
|
}
|
|
|
|
|
|
void OnTriggerEnter2D(Collider2D collision)
|
|
{
|
|
if (collision.tag == "PlayerHitbox")
|
|
{
|
|
inRange = true;
|
|
firstAttack = false;
|
|
}
|
|
|
|
if (collision.tag == "AttackHitbox")
|
|
{
|
|
//TakeDamage(1.0f);
|
|
}
|
|
}
|
|
|
|
void OnTriggerStay2D(Collider2D collision)
|
|
{
|
|
if (collision.tag == "PlayerHitbox")
|
|
{
|
|
inRange = true;
|
|
}
|
|
}
|
|
|
|
void OnTriggerExit2D(Collider2D collision)
|
|
{
|
|
inRange = false;
|
|
timer = 0f;
|
|
}
|
|
|
|
public void TakeDamage(float damage)
|
|
{
|
|
health = health - damage;
|
|
if (health <= 0)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|