- Home /
Trigger not triggering on Collision2D
Hey all,
I have a player with a RigidBody2D, Capsule Collider 2D and a characterController script. image
And an empty gameobject with a boxcollider2D with the layer set to "ladder" image
And in the characterController script I use the following code :
void OnTriggerEnter2D(Collision2D trigger)
{
Debug.Log("LadderCollider : Hit by Player");
if (trigger.gameObject.tag == "ladder" && Input.GetKeyDown(KeyCode.W))
{
onLadder();
}
}
void onLadder()
{
Debug.Log("Player : I'm on a Ladder");
_rigidBody2D.gravityScale = 0f;
}
But I cant get Debug.Log("LadderCollider : Hit by Player"); & Debug.Log("Player : I'm on a Ladder"); both to fire, any ideas what I'm messing up?
Thanks in advance, Olivier
Answer by MacDx · Jul 03, 2018 at 11:53 PM
OnTrigger methods only work with collider components that have the IsTrigger option checked and your player doesn't so your code wont run at all. Either change the method to OnCollisionEnter2D or check that box on the player's collider component
Hope this helps!
Edit: I just spotted another problem. Your OnTriggerEnter2D method has a Collision2D parameter and that is wrong. OnTriggerEnter2D should have a Collider2D parameter instead. Here's the documentation:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
Your second answer was correct! Thanks a bunch, I've been butting my head at this for hours and you fixed it.
Cheers, Olivier
Glad I could help! Please mark the answer as accepted.
Regards!