Enemy CharacterController stopped detecting all triggers, but Player's CharacterController still detects and fires the same triggers
I have a Player prefab with a Character Controller component that when it overlaps the trigger for a pressure plate, the OnTriggerEnter method is called. However, I also have a Sheep prefab with a Character Controller component that does not call the OnTriggerEnter method though it did yesterday (despite not changing any properties for this pressure plate or Sheep's triggers script).
I'm at a total loss as I have tried everything from ensuring the method declaration is spelled correctly private void OnTriggerEnter(Collider other)
to making sure the "isTrigger" box is ticked for the BoxCollider on the pressure plate. I also tried adding a RigidBody that is kinematic to the Sheep prefab and then to the Pressure Plate prefab but to no avail. Any help would be greatly appreciated.
Sheep Prefab:
The Sheep prefab has just a CharacterController with the exact same property settings as the Player prefab other than the height. As I understand it, the CharacterController component has a RigidBody so I don't need to add one for the collision system to work (as I have done with the Player).
Pressure Plate Prefab:
Each Cube on the Pressure Plate including the base has a BoxCollider with "isTrigger" unticked. Only the Trigger child GameObject has a BoxCollider with "isTrigger" ticked.
PlayerTriggers.cs (attached to Player)
public class PlayerTriggers : MonoBehaviour
{
PlayerMovement movement;
private void Awake()
{
movement = GetComponent<PlayerMovement>();
}
private void Start()
{
Physics.IgnoreLayerCollision(gameObject.layer, Layers.SHEEP);
}
private void OnTriggerEnter(Collider other)
{
print("PLAYER ON TRIGGER ENTER------------------------");
if (other.CompareTag(Tags.SPRING))
{
movement.Spring(other.GetComponent<Spring>().GetForce());
}
}
}
SheepTriggers.cs (attached to Sheep):
public class SheepTriggers : MonoBehaviour
{
private SheepMovement movement;
private void Awake()
{
movement = GetComponent<SheepMovement>();
}
void OnTriggerEnter(Collider other)
{
print("TRIGGER---------------------------");
if (other.CompareTag(Tags.SPRING))
{
movement.Spring(other.GetComponent<Spring>().GetForce());
}
if (other.CompareTag(Tags.PRESSURE_PLATE))
{
movement.SetIdle(true);
}
if (other.CompareTag(Tags.CONVEYOR_BELT))
{
movement.AddVelocity(other.GetComponent<ConveyorBelt>().GetVelocity());
}
}
}
When the Player moves onto the Pressure Plate, the debug console prints PLAYER ON TRIGGER ENTER------------------------
, but for the Sheep it prints nothing. Any ideas? Is there something I'm missing here? Thanks so much!
Answer by LoopusLazuli · Feb 25, 2021 at 04:32 PM
The issue had to do with collision layers. The Sheep prefab is on the "Sheep" layer and based on the Layer Collision Matrix in the Project Settings (Layer-based Collisions), the Sheep would only register collisions with one another. By setting the Sheep's collision layer to Default, OnTriggerEnter worked once again.
In summary, if your triggers aren't registering collisions, there are a few things to try:
Ensure the OnTriggerEnter method declaration is correct using autocomplete or otherwise. The correct declaration is
private void OnTriggerEnter(Collider other) {}
.Only one of the colliders can be a trigger ("isTrigger" property is enabled). PhysX does not recognize collisions between two triggers (Collider.OnTriggerEnter).
One of the GameObjects involved in the collision must have a RigidBody (can be kinematic), or a Character Controller component.
And finally, be mindful of your collision layers and how they interact. Are these collisions ignored? Are they enabled through Layer Collision Matrix (as detailed above)?
Hopefully this helps someone. Happy developing everyone! Make something awesome!
Your answer
Follow this Question
Related Questions
FMOD Parameter Change Triggers Not Working, but only in one Scene? 1 Answer
Assiging character controllers capsule collider to cloth? 2 Answers
Why is OnTriggerExit not firing? 3 Answers
My gameobjects collide though they are not near eachother 1 Answer
CharacterController gets stuck between two or more box colliders 0 Answers