2D Collision Between Two Objects Not Working

Hi I’ve been following this tutorial on Youtube and I am having problems with object collisions. After Googling and asking Chat GPT quesitons about my issue, I checked on the following things:


  1. Collider Components:
  • Ensure Both Objects Have Colliders: Check that both the horse and the clouds have collider components. From your screenshots, the horse has a Circle Collider 2D, and each cloud has a Box Collider 2D. This setup should work, so make sure none of the colliders are disabled.
  • Collider Sizes and Positions: Sometimes, colliders might be too small or positioned incorrectly, causing them not to overlap even if the visuals do. Make sure the collider size is appropriate and covers the visible area of the cloud or horse.
  1. Rigidbody2D Component:
  • Add Rigidbody2D to One of the Objects: Unity’s physics engine requires at least one of the colliding objects to have a Rigidbody2D component for collisions to register. The horse already has a Rigidbody2D component, which is good. Make sure the clouds don’t also have a Rigidbody2D, or set them to Kinematic if they do, to prevent them from moving unexpectedly.
  • Collision Detection Mode: In the Rigidbody2D component on the horse, ensure the Collision Detection mode is set to Continuous if your horse is moving fast. This can help with collisions that might be missed in fast-moving objects.
  1. Layer Collision Matrix:
  • Layer Compatibility: If the horse and clouds are on different layers, make sure that their layers are set to collide in Unity’s collision matrix. Go to Edit > Project Settings > Physics2D and ensure the layer of the horse is set to collide with the layer of the clouds.
  • Tags and Layers: Double-check that the clouds and horse are not on layers that are set to ignore each other. Sometimes, using Ignore Raycast or a custom layer with collision disabled can cause this issue.
  1. Is Trigger:
  • Ensure ‘Is Trigger’ Is Not Checked: For standard collisions, make sure that the Is Trigger option is unchecked on the clouds’ Box Collider 2D components. Is Trigger would cause the objects to register trigger events instead of collisions, which may not be what you’re aiming for in this case.
  1. Script Logic:
  • Check the Script for Errors: The OnCollisionEnter2D method in the Horse script should work for collision detection. Make sure there are no errors in the script that could prevent this method from being called, and confirm that the logic is correctly connected to the horse GameObject.
  1. Scene View:
  • Enable Gizmos: In the scene view, enable Gizmos to visualize the colliders. This can help you see if the colliders are overlapping as expected when the horse and clouds are close together.

However I couldn’t find out why they are not colliding yet. Please help me figure out why it’s not working. I’ll attach a few screenshots (edit: it seems like I can only put one ss since it’s my first post) and code snippets below. Thank you!

using UnityEngine;

public class Horse : MonoBehaviour
{

public Rigidbody2D myRigidBody;
public float flapStrength;
public LogicScript logic;
public bool birdIsAlive = true;

// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
    logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Space) && birdIsAlive)
    {
        myRigidBody.linearVelocity = Vector2.up * flapStrength;
    }
}

private void OnCollisionEnter2D(Collision2D collision)
{
    logic.gameover();
    birdIsAlive = false;
}

}

Collisions in 2D definitely work. If you doubt it, make a quick 2D collision demo scene with two sprites with colliders, one with a Rigidbody2D and a little stub collider and work through the official Unity3D docs on this stuff, even using their example script if you like.

Once that is working, bisect back to what you have and find what part differs.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.