Problem with the OntriggerExit2D

i’m trying to make my enemy to turn around when he hits a wall, but the thing is, theres nothing detected. even when i try on trigger enter, it doesnt seem to detect any part of the tilemap. also, I really dont get it when you say that the collider will exit the trigger when going through the wall, as he now collide with the wall and the floor(that is not a floor tile anymore but still a tile… )

Try OntriggerEnter2d with debug logs and maybe show you code as well now that I think about it

Hi,

The tilemap collider is an edge collider meaning the thin line is the collider. If the enemy’s “periscope” collider is set to “Is Triiger” and if it touches that edge, the OnTriggerEnter2D method gets called. And when it leaves the edge, the OnTriggerExit2D method gets called.

I’m not sure if this would help you but here’s how I handled wall and edge detection. I created two colliders one that hangs out just past the enemy to touch a wall, and one up and down out past the enemy to check if there’s a tile underneath them. I had to add a timer because it would jitter back and forth between not touching and touching.
image

edit: that debug.log I left in there is how I noticed what was happening as it was shaking back and forth on the edge lol

using UnityEngine;

public class EnemyMovement : MonoBehaviour
{
    [SerializeField] float moveSpeed = 1f;
    [SerializeField] BoxCollider2D edgeDetector;
    [SerializeField] CapsuleCollider2D wallDetector;

    Rigidbody2D slimeRB;
    float flipTimer;
    float flipReset = .5f;

    void Start()
    {
        slimeRB = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        slimeRB.velocity = new Vector2(moveSpeed, 0f);
        FlipSprite();
        WallLedgeCheck();
    }

    private void WallLedgeCheck()
    {
        flipTimer += Time.deltaTime;
        if (flipTimer < flipReset) { return; }
        if (wallDetector.IsTouchingLayers(LayerMask.GetMask("Ground")) || (!edgeDetector.IsTouchingLayers(LayerMask.GetMask("Ground"))))
        {
            Debug.Log("colliders check passed");
            moveSpeed = -moveSpeed;

            flipTimer = 0;
        }

    }

    private void FlipSprite()
    {
        slimeRB.transform.localScale = new Vector2(Mathf.Sign(slimeRB.velocity.x), 1f);
    }
}```

Maybe this happened? Rick said to adjust the collision matrix within “Edit → Project Settings → Physics 2D” of the “Enemies” layer to only collide with the “Player” layer but then quickly catches his mistake and says “Ground” layer is also needed but it slipped past me when I first watched the lecture. The enemy needs to also have collision with the “Ground” layer which contains the “Tilemap Collider 2D” of the “Platforms Tilemap” that the “OnTriggerExit2D()” method is looking for. This was my issue but I hope this helps!

unknown-1

Good luck!

Lucid

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms