Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnTriggerEnter2d doesn't work without a player move before

Discussion in '2D' started by bylduda, Jun 26, 2022.

  1. bylduda

    bylduda

    Joined:
    Apr 13, 2022
    Posts:
    4
    Hi, I'm trying to make my player attack, so I found some example implementation and there is one:
    so I'm following this video:

    there is a github:
    https://github.com/Tutorials-By-Kaupenjoe/Basic-Unity-2020.3-Tutorials/tree/8-playerAttacking/Assets

    The problem is OnTriggerEnter2D only works once; to work it again I have to move a player.
    So if I stay, doesn't affect enemy more than once. In inspector I see its called again, but it does nothing to enemy.
    Code (CSharp):
    1. public class AttackArea : MonoBehaviour
    2. {
    3.     [SerializeField] private int damage = 3;
    4.  
    5.    private void OnTriggerEnter2D(Collider2D collider)
    6.     {
    7.         if(collider.GetComponent<Health>() != null)
    8.         {
    9.             Health health = collider.GetComponent<Health>();
    10.             health.Damage(damage);
    11.         }
    12.     }
    Someone in comments has the same issue, in video works as it should be. Cannot move futher without attack script stucked second day.
    Any one know why this works like that?
     
  2. ronJohnJr

    ronJohnJr

    Joined:
    Mar 5, 2015
    Posts:
    106
    you probably want ontriggerstay
     
    bylduda likes this.
  3. bylduda

    bylduda

    Joined:
    Apr 13, 2022
    Posts:
    4
    As I tried its even weired.


    Fixed it. The AREA ATTACK hadn't Rigidbody2d. I though like inisible area of attack doesn't need it. The thing I don't understand why it worked without RB2D but only noticing its once.

    Thanks for answer!

    SS:
    upload_2022-6-26_12-42-7.png
     
    ronJohnJr likes this.