- Home /
Problem: One Trigger activating a seperate Trigger it is not in contact with.
I have this problem that seems really counter-intuiative. There is a trigger on the bottom of a large crate (to test if it should crush an object it falls on), and another trigger on the bottom of the player model (to test if the player 'onGround').
When the player model enters the crate crush trigger, the player 'onGround' trigger is activated.
This is a big problem, because if the player gets hit by the crate crush trigger, the code checks if the player is 'onGround', and if so the player is crushed and gameover.
Here is a video of this problem in action: https://vimeo.com/611291709
Here is the trigger function on the player:
//Check if player is on the ground
private void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("Ground") || other.CompareTag("BigObject"))
{
onGround = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Ground") || other.CompareTag("BigObject"))
{
onGround = false;
canDoubleJump = true;
}
}
And here is the trigger function on the crate:
private void OnTriggerStay(Collider other)
{
if (other.CompareTag("RegularObject"))
{
Destroy(other.gameObject);
}
if (other.CompareTag("Player"))
{
playerController = player.GetComponent<PlayerController>();
if (playerController.onGround)
{
Debug.Log("Player Crushed!");
GameManager.Instance.PlayerCrushed();
}
}
}
Does the OnTrigger function get called on the player not only when their attached trigger is entered, but also when the player enters other objects triggers?
Is it better to use raycasts for some of these checks?
I think you might be having the same problem as this guy https://answers.unity.com/questions/1294855/is-there-a-way-to-make-ontriggerenter-work-only-fo.html
In that case, you need to separate your colliders with Empty Child objects
Answer by Chimz · Sep 29, 2021 at 12:49 AM
Alright I think I got your answer now. Your problem is that even though you have codes for Colliders and Triggers (which work different), the fact that they're both Colliders at base and overlap can cause such bugs. To separate the two, it is best if you do this: You've put the Colliders on the Parent Object and the Triggers on child Objects. You've already done the first step. Now, on those child Objects you need to choose Specific Tags. Choosing which objects are Wakable with tags is not suggested. I suggest using Layers and setting them up in the Project Setting, because we need the Tags for other tasks. Now, you've set the layers and now it's time for the Specific Tags. For example, for the player Ground Trigger Object, just put "Ground Checker" and for the Box's crush trigger Object, put "Crush" Tag. Now, in your scripts, on the OnTriggerEnter's you need to check for the Tag of the Trigger that you've collided with. If it matches what you want, like having a Walkable Tag, then you can set your Grounded Boolean to True. If the tag is Crush, and the player is Grounded, then you Crush the player. Here's your Player code, edited to fit. But I have brought the "Crush Check" into the Player's Script for easier use: //Check if player is on the ground private void OnTriggerStay(Collider other) { if (other.CompareTag("Crush") && onGround) { Debug.Log("Player Crushed!"); GameManager.Instance.PlayerCrushed(); } }
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Ground") || other.CompareTag("RegularObject"))
{
onGround = false;
canDoubleJump = true;
}
}
This looks really great, thanks for all the effort you put into looking into this! I'll have a detailed look sometime, but it looks good!!