Search Unity

Resolved Collider gets skipped by player (without moving fast)

Discussion in 'Scripting' started by YoloBoy864, Jun 13, 2022.

  1. YoloBoy864

    YoloBoy864

    Joined:
    Aug 12, 2021
    Posts:
    39
    So, I want the game to do something everytime the player steps on a new tile, but sometimes when it does this, it doesn't trigger.

    video showcase: (notice how when the player moves on a new tile it changes the layout (good) but sometimes it doesn't until the player steps on a second tile)


    I have also noticed that when you enter a second tile (after the first one didn't trigger) it than does trigger the first action at the same time with the second action (I made this visual by adding a number up in the left corner of the video, the number skips from 2->4).

    All the colliders on the tiles are the same size and the idea is to actually have the center tile always on the player.

    code that does something wrong for some reason:
    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (currentTile != collision.gameObject)
    4.         {
    5.             loadLand.ReloadChunks(collision.gameObject);
    6.  
    7.             //test
    8.             i += 1;
    9.             Debug.Log(i);
    10.         }
    11.         currentTile = collision.gameObject;
    12.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Is this perhaps your problem?

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
  3. YoloBoy864

    YoloBoy864

    Joined:
    Aug 12, 2021
    Posts:
    39
    I switched to onTriggerStay2D and that fixed it
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    You shouldn't have to do that unless it's the same collider which from your description it isn't. If each of those squares is a separate collider then you'll get OnTriggerEnter2D. I don't follow how using OnTriggerStay2D would fix it because you won't get this until a OnTriggerEnter2D has been called.

    I'm very curious to see this set-up.