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. Dismiss Notice

Problem detecting two objects with OnTriggerEnter2D

Discussion in 'Scripting' started by Feretro, Dec 7, 2019.

  1. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Hello!
    Here's my problem! I have a ground that I tagged as "Ground", so if you are not on the ground, player could not jump.

    Now ... I wanted to make my enemy jump so I thought i could use some "jump points" and so I did!

    Created empty objects, tagged as "JumpPoint" and inserted this on my enemy script:

    Code (CSharp):
    1. public void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         Debug.Log("Collision with: " + collision.tag);
    4.         if (collision.tag == "JumpPoint")
    5.         {
    6.             enemyRb.AddForce(new Vector2(enemyRb.velocity.x, enemyJumpForce));
    7.         }
    8.     }
    That "Debug" says: groundgroundgroundgroundgroud.... and never "JumpPoint". So my enemy never jumps!

    Where's my mistake this time? :D:D

    As usual, thanks for your precious help!

    P.s.
    Yes! I am sure I tagged jumpPoints! :p
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @Feretro,

    You said "I am sure I tagged jumpPoints" but in your code you write "JumpPoint" so are you 100% sure you're written those names exactly correct.

    Also, when you say "empty objects", did you add triggers to them?
     
    Feretro likes this.
  3. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Hi Olmi! :D
    Yes, yes, names are correct but .... ehm ... :oops: no, I have no trigger on those "empty object" ... don't tell me .....

    Oh ... yes, you told me! Now It works .... It jumps!!! .... In a really strange way but it works!

    Thanks for your help!!!!
     
    Olmi likes this.