2
\$\begingroup\$

In my 2D game, there is a ball with a circle collider ,and a platform with a box collider.

The ball has a material for bounciness and a rigidbody2D. The platform only has the box collider.

I find it to be as simple as could be, but it's not working for some reason, so apparently it's not that simple. Here's the C# code snippet found in the ball class which is attached to the ball game object:

void OnCollisionEnter(Collision other)
{
    Debug.Log("Hit");
    animator.Play("Bounce");
}

Why isn't the function being called on collision? Any idea would be appreciated.

\$\endgroup\$
4
  • \$\begingroup\$ Set the isTrigger property to True in the collider you want to trigger this event \$\endgroup\$ Aug 21, 2016 at 2:20
  • \$\begingroup\$ there's a problem with that, the ball then goes through the gameobject, I want it to bounce off the platform, not go through it. \$\endgroup\$
    – Alox
    Aug 21, 2016 at 2:25
  • \$\begingroup\$ Can you confirm that when you say "box collider", you mean "box collider 2D"? Don't be confused by the comment made by @LuisMasuelli; you only want your collider to be set as a trigger if it actually acts as a trigger. Setting it to isTrigger = true will not likely fix your problem, but rather cause more problems. \$\endgroup\$
    – Gnemlock
    Aug 21, 2016 at 2:37
  • \$\begingroup\$ yes, everything is the 2d ones. Box Collider 2D, Rigidbody 2D, Circle Collider 2D \$\endgroup\$
    – Alox
    Aug 21, 2016 at 3:05

1 Answer 1

5
\$\begingroup\$

Use collision events for 2D. You are using the events for 3D collision detection where your colliders and rigidbody components are for 3D.

void OnCollisionEnter2D(Collision2D other)
{
    Debug.Log("Hit");
    animator.Play("Bounce");
}
\$\endgroup\$
1
  • \$\begingroup\$ perfect, damn, I think I did almost that but with a lowercase d or something which caused it to not work. Sorry abou that :P but thank you! \$\endgroup\$
    – Alox
    Aug 21, 2016 at 4:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .