I have an issue with my script.The enemy is supposed to walk towards you when you get into his range and if you are close enough, the enemy will attack.The problem is, the enemy is not doing anything at all.I checked my script and cannot see the issue.Tried some Debug.Log messages to see something and everything seems to be fine.I dont even get any script errors.Is there something i missed?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBehaviour : MonoBehaviour,IDamegable
{
public int Health { get; set; }
private float staggerTime;
public float startStaggerTime;
[SerializeField]
protected float health;
#region Public Properties
public Transform rayCast;
public LayerMask raycastMask;
public float rayCastLenght;
public float attackDistance; //minimum distance for attack;
public float movespeed;
public float timer; //Delay between attacks
#endregion
#region Private Properties
private RaycastHit2D hit;
private GameObject target;
private Animator anim;
private float distance; //distance between player and enemy
private bool attackMode;
private bool inRange; //if in range
private bool cooling; //enemy cooldown after attack;
private float intTimer;
#endregion
void Awake()
{
intTimer = timer;
anim = GetComponent<Animator>();
}
private void Update()
{
if (inRange)
{
hit = Physics2D.Raycast(rayCast.position, Vector3.left, rayCastLenght, raycastMask);
RaycastDebugger();
}
//when Player Is Detected
if (hit.collider != null)
{
EnemyLogic();
}
else if (hit.collider == null)
{
inRange = false;
}
if (inRange == false)
{
anim.SetBool("CanWalk", false);
StopAttack();
}
#region Stagger
if (staggerTime <= 0)
{
//normal speed
}
else
{
//speed 0
staggerTime -= Time.deltaTime;
}
#endregion
}
void OnTriggerEnter2D(Collider2D trig)
{
if (trig.gameObject.tag == "Player")
{
target = trig.gameObject;
inRange = true;
}
}
void EnemyLogic()
{
distance = Vector2.Distance(transform.position, target.transform.position);
if (distance > attackDistance)
{
Move();
StopAttack();
}
else if (attackDistance >= distance && cooling == false)
{
Attack();
}
if (cooling)
{
Cooldown();
anim.SetBool("Attack", false);
}
}
void Move()
{
anim.SetBool("CanWalk", true);
if (!anim.GetCurrentAnimatorStateInfo(0).IsName("Enemy_Attack"))
{
Vector2 targetPosition = new Vector3(target.transform.position.x, transform.position.y);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, movespeed * Time.deltaTime);
}
}
void Attack()
{
timer = intTimer;
attackMode = true;
anim.SetBool("CanWalk", false);
anim.SetBool("Attack", true);
Debug.Log("ATTACKING");
}
void StopAttack()
{
cooling = false;
attackMode = false;
anim.SetBool("Attack", false);
}
public void RaycastDebugger()
{
if (distance > attackDistance)
{
Debug.DrawRay(rayCast.position, Vector3.left * rayCastLenght, Color.red);
}
else if (attackDistance > distance)
{
Debug.DrawRay(rayCast.position, Vector3.left * rayCastLenght, Color.green);
}
}
public void TriggerCooling()
{
cooling = true;
}
void Cooldown()
{
timer -= Time.deltaTime;
if (timer <= 0 && cooling && attackMode)
{
cooling = false;
timer = intTimer;
}
}
public void Damage(float damage)
{
health -= damage;
staggerTime = startStaggerTime;
Debug.Log("Took damage");
if (health <= 0)
{
Destroy(gameObject);
Debug.Log("Died");
}
}
}
