Answer the question
In order to leave comments, you need to log in
It is necessary that the enemy hit every three seconds and only when touching the player. How to do it?
It seems that I did everything right, the enemy hits and only on contact, but for some reason only once, then - nothing at all. I decided to make the break time between beats using the while loop, but for some reason it still does not work. I can send the code if needed. Please help, I'm still a newbie
Answer the question
In order to leave comments, you need to log in
Both should have a collider with no trigger ticked
public bool atack = false;
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag== "vrag")
{
atack = true;
}
}
public void FixedUpdate()
{
if(atack){
int time = 0;
time += 1;
if(time == 3)
{
time = 0;
//Делаешь атаку
}
}
}
While it is better not to use and do a timer attack as in the example above.
More or less like this
private void OnCollisionStay(Collision other)
{
go = other.gameObject;
if (!go.CompareTag("Player"))
return;
Attack();
}
private void Attack()
{
if (gameEntity.isEnemyAttack)
{
gameEntity.isEnemyAttack = false;
gameEntity.basicEnemy.Cooldown = cooldown;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question