A
A
AunPra2019-04-02 19:13:39
2D
AunPra, 2019-04-02 19:13:39

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

3 answer(s)
S
Sergey Ivanov, 2019-04-03
@keksmr

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;
                   //Делаешь атаку
               }
       }
}

C
CHIDWI, 2019-04-04
@CHIDWI

While it is better not to use and do a timer attack as in the example above.

A
Ark Tarusov, 2019-04-16
@kreo_OL

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;
        }
    }

In the update, count the timer and when the time is reached, do gameEntity.isEnemyAttack = true;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question