0
\$\begingroup\$

My problem is that I have a player with collisions, and enemy can collide with players collision and execute Hurt function in players script. But the problem is enemies can overlap and can hit the player in the literally same frame. So they both execute Hurt function.

In my Hurt function I change players collision layer for a 2 seconds, and he is no longer be able to seen by enemies. But it doesnt matter because of the same hit by two or more enemies in the same frame.

How can I solve this? I tried bool to check if Hurt function is executed but it doesnt work too...

Here is :

Code on Enemy:

 IEnumerator OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        //burası kediler üst üste bindiginde birden fazla kez calisabiliyor
        charMovementRef.StartCoroutine(charMovementRef.Hurt());

        Debug.Log("hasar yedi");
    }
}

Code on Player:

//Hurt should only works one at the same time
public IEnumerator Hurt()
{
    //layer değiştirerek düşman collisionlarından kaç
    gameObject.layer = 9;
    //canı azalt
    health--;
    HeartUI(); //show the new health on UI
    //karakter görüntüsü yanıp sönsün
    gameObject.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 0.5f);
    //playerı zıplat
    rb2D.velocity = new Vector2(rb2D.velocity.x, 0);
    rb2D.AddForce(new Vector2(0, jumpPower / 1.2f));

    //2 saniyeliğine colliderları kapalı kalsın, sonra eski haline çevir
    yield return new WaitForSeconds(2);
    gameObject.layer = 0;
    gameObject.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f);

}
\$\endgroup\$
  • 2
    \$\begingroup\$ "How can I solve this? I tried bool to check if Hurt function is executed but it doesnt work too..." This sounds like it should work, ideally the player can have a boolean that checks if the player can be hit by enemies. Once an enemy touches them, they get hurt, and can't be hurt for another two seconds, which turns the boolean, so even if another enemy touches them on the same frame, the player can still not be hurt. \$\endgroup\$ – TomTsagk Jan 7 at 17:21
  • \$\begingroup\$ I think it doesnt matter whether there is a code or not, I need some kind of thinking logic for this. And bool is not working because two enemies executes same Hurt function and uses bool at the same time. After that they cant hit them for 2 sec but when they attack and hurt, two of them do this at the same time. And because of that player health goes 3 to 1, instead of 2. \$\endgroup\$ – Enes Tulek Jan 8 at 16:03
  • \$\begingroup\$ I don't see where you're setting or checking your bool to determine whether Hurt should fire. Is that code elsewhere? \$\endgroup\$ – DMGregory Jan 9 at 18:21
  • \$\begingroup\$ @DMGregory Code on Enemy, OnCollisionEnter, StartCoroutine(charMovement.Hurt()); \$\endgroup\$ – Enes Tulek Jan 10 at 18:22
  • \$\begingroup\$ There's no boolean variable setting or checking there. So where is it? \$\endgroup\$ – DMGregory Jan 10 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.