I have an enemy and he has a capsule collider (So that it is not transparent) and there is also a circle collider, which should ideally provide a trigger for the attack radius, but it only works once and then, being inside this collider, it no longer hits. Another problem is that if the character goes towards the enemy, he will fly away and from there there is a new problem, that the running animation disappears.
Share your code, use code tags. My first guess is you are using OnTriggerEnter2D which calls ONCE when the right thing enters the trigger. Then you have OnTriggerExit2D which calls ONCE when it exits. Then there is OnTriggerStay2D which calls every frame that the thing collides with the trigger. However, what you probably want to do is have a target variable that gets set to the Player when he enters the trigger, and then you set the target to null if he exits. Then you do a null check in your attack function to see if there is a target or not.
Regardless, share your code so we can help
Here a code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int health;
public float speed;
//private Animator Eanim;
public GameObject BloodEffect;
private float timeBtwAttack;
public float startTimeBtwAttack;
public int damage;
private float stopTime;
public float startStopTime;
public float normalSpeed;
private Player Player;
// Start is called before the first frame update
void Start()
{
//Eanim = GetComponent();
Player = FindObjectOfType();
normalSpeed = speed;
}
// Update is called once per frame
void Update()
{
if(stopTime <= 0)
{
speed = normalSpeed;
}
else
{
speed = 0;
stopTime -= Time.deltaTime;
}
if(health <= 0)
{
Instantiate(BloodEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
if(Player.transform.position.x > transform.position.x)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
transform.position = Vector2.MoveTowards(transform.position, Player.transform.position, speed * Time.deltaTime);
}
public void TakeDamage(int damage)
{
stopTime = startStopTime;
timeBtwAttack = startTimeBtwAttack;
Instantiate(BloodEffect, transform.position, Quaternion.identity);
health -= damage;
}
public void OnTriggerStay2D(Collider2D other)
{
if(other.CompareTag(“Player”))
{
if(timeBtwAttack <= 0)
{
Eanim.SetTrigger(“Attack”);
}
else
{
timeBtwAttack -= Time.deltaTime;
}
}
}
public void OnEnemyAttack()
{
Instantiate(BloodEffect, Player.transform.position, Quaternion.identity);
Player.health -= damage;
timeBtwAttack = startTimeBtwAttack;
}
}
I forgot to remove comments when I posted, but even if I remove them nothing will change btw
As said above, please use code-tags .
Throw in some Debug.Logs. My initial guess is timeBtwAttack isn’t getting reset properly. Or, you are setting the Attack animation trigger but not setting it back. Again, throw in some debug.logs to check the values of some of these fields to see what is actually going on. Bonus for you:
public void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Debug.Log("Player is within trigger of enemy");
if (timeBtwAttack <= 0)
{
Eanim.SetTrigger("Attack");
Debug.Log("timeBtwAttack is: " + timeBtwAttack);
}
else
{
timeBtwAttack -= Time.deltaTime;
Debug.Log("timeBtwAttack is: " + timeBtwAttack + "And should start decreasing again");
}
}
}
public void OnEnemyAttack()
{
Instantiate(BloodEffect, Player.transform.position, Quaternion.identity);
Player.health -= damage;
timeBtwAttack = startTimeBtwAttack;
Debug.Log("Enemy has attacked, decrease player health, and reset timeBtwAttack. timeBtwAttack is now: " + timeBtwAttack);
}
It is just a couple of your functions, but that is what i mean by using Debug.Logs (and using code tags…go and edit your post please)
Hello, thanks for your help! I found by mistake that I set the animation transition incorrectly, instead of connecting from the running animation to the attack animation (If the trigger is activated), I set the attack animation from anystate:smile: