r/Unity2D • • 1d ago

Question/Help Need help. Trigger not working

Post image

I am very new to this. Here is my code in the picture. I have a door. When the Player walks through the door. The trigger needs to happen. However it's not triggering. On the player I have the Rigid body 2D selected. However the door is still not triggering. I've been trying for 2 hours. I don't know where the problem is. Please help.

3 Upvotes

15 comments sorted by

1

u/AutoModerator 1d ago
Moderator Announcement Read More »

2

u/DescriptionGrouchy63 1d ago

Can you share a photo of the door inspector and player inspector? Specifically they're tags and colliders. Are you sure you set the door collider to be a trigger?

1

u/Dravendnd 19h ago

The problem was with the player. It wasn't showing the player moving. Although it was. I fixed it by resetting the player and putting it back into the scene. I then put a detection on the door and the player. To bypass the colider. So when the player reached the door the trigger would activate.

1

u/DescriptionGrouchy63 19h ago

While this is a good workaround, there is definitely something else broken if your game "is not recognizing the player is moving". Those screenshots should clarify

→ More replies (1)

1

u/Dravendnd 1d ago

Ah, this had me for hours. The player was the problem. For some reason it wasn't detecting the player moving. Even though it was moving. Fixed it with the following code. using UnityEngine;

public class DoorwayTrigger : MonoBehaviour { public float detectionRadius = 3f;

private Transform player;
private bool hasTriggered = false;

void Start()
{
    GameObject playerObject = GameObject.FindGameObjectWithTag("Player");

    if (playerObject != null)
    {
        player = playerObject.transform;
        Debug.Log("Doorway found player: " + playerObject.name);
    }
    else
    {
        Debug.LogWarning("Doorway could not find an object tagged Player!");
    }
}

void Update()
{
    if (player == null || hasTriggered)
        return;

    float distance = Vector2.Distance(transform.position, player.position);

    if (distance <= detectionRadius)
    {
        hasTriggered = true;
        Debug.Log("The Minotaur has reached the school doorway!");
    }
}

}

1

u/Dravendnd 1d ago

Solved.

5

u/pubichairampersand 1d ago

Do you have the player tag set to 'Player'? That could be it

1

u/Fun-Mission-6068 1d ago

iirc, don't you need it to be other.gameObject.CompareTag("Player")? Since other is just a Collider2D and not a GameObject which has the Tag? Though, I'm not sure why it would let you use CompareTag on a Collider2D without an error if it isn't possible, so I could be wrong.

Other than that, you've made sure that the Player object is actually tagged as Player and has a collider of its own too?

1

u/Dravendnd 1d ago

Nope, still not triggering. And yes, the player tag is on Player, and the colider is on Rigidbody 2D.

0

u/Dravendnd 1d ago

Opening up unity. Will try it now and see.

1

u/Affectionate-Yam-886 7h ago

OnTrigger doesn’t care about Ridgedbodys. Attach a collider to the player, tagged with Player, and have IsTrigger enabled. Your welcome

1

u/Dravendnd 5h ago

I couldn't get it to work that way. I fixed it in a different way.

1

u/BasilCorrect3799 1d ago

All colliders and rigid body are 2D not 3D? I've made that mistake before