Home > Mobile >  OnCollisionEnter2D is being called with platform effector even though I haven't collided with i
OnCollisionEnter2D is being called with platform effector even though I haven't collided with i

Time:12-07

I have a moving platform script. My platform is frozen unless I collide with it, then it starts moving. I have a platform effector so I can be able to jump on it from under it and not hit my head on it. However OnCollisionEnter2D is getting called even though I'm under it and haven't physically collided with the one way collision. It's as if it's a trigger.

   private void OnCollisionEnter2D(Collision2D collision)
    {
        
        if (collision.gameObject.GetComponent<Player>())
        {
            Debug.Log("On Collision Enter");
            //CameraFollow.instance.useFixedDeltaTime = false;
            Player = collision.gameObject;
            PlayerScript = Player.GetComponent<Player>();
            amountOfTimesPlayerCollided  ;
        }

Even if my head just touches the collider area (without hopping on the platform) the method gets called and "On Collision Enter" is logged into the console. How do I make it so that OnCollisionEnter2D only gets called when I physically collide with the platform?

Thanks! I'm using the latest version of Unity 2021 but this has happened to me with other versions in the past I'm pretty sure.

CodePudding user response:

This callback is sent when an incoming collider makes contact with this object's collider.

I think "contact" and "touch" have the same meaning, so this is an expected behavior.

only gets called when I physically collide with the platform?

I guess you want to detect an impact, so you may also pay attention to the velocity between two objects when the collision occurs.

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.GetComponent<Player>()
        && collision.relativeVelocity.magnitude > THRESHOLD)
    {
        ......
    }
}
  • Related