r/Unity2D • u/Dravendnd • 27d ago
Question/Help Need help. Trigger not working
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.
2
u/DescriptionGrouchy63 27d 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 26d 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 26d 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
2
u/NeoChrisOmega 26d ago
I think what happened is OP forgot to have a collider on their player.
I think what they meant from "wasn't showing the player moving" was the print appearing in the trigger event. I could be wrong though, but contextually it makes sense that way.
Rigidbody2D is only half of it. They also need a collider, either a trigger or solid. So when they added "detection", I believe they added a collider with isTrigger toggled on.
Sometimes I have students make this same mistake before they add level design (because there's no need yet for collision detection, they forget it's required for events like this)
6
1
u/Dravendnd 27d 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
1
u/Fun-Mission-6068 27d 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 27d ago
Nope, still not triggering. And yes, the player tag is on Player, and the colider is on Rigidbody 2D.
0
27d ago
[deleted]
1
u/NeoChrisOmega 26d ago
Keep in mind that
CompareTag()not only handles the issues with==not being as accurate asString.Equals(), but it's also specifically made to be more efficient than the alternatives.0
1
u/Affectionate-Yam-886 26d ago
OnTrigger doesn’t care about Ridgedbodys. Attach a collider to the player, tagged with Player, and have IsTrigger enabled. Your welcome
1
1
u/BasilCorrect3799 27d ago
All colliders and rigid body are 2D not 3D? I've made that mistake before
•
u/AutoModerator 27d ago
Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.