Rigidbodies Inside CollidersHow to hack 2D colliders to contain RigidBody objects

Author Waldo
Published August 1, 2019

In this tutorial I show you how to hack any 2D collider to allow it to contain objects with RigidBodies.

Video Walkthrough

  • 0:00 - Explaining the Problem
  • 1:20 - Theorizing a Solution
  • 2:11 - Testing The Theory
  • 3:45 - The Final Code Used

Source Code for EdgeColliderHack.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EdgeColliderHack : MonoBehaviour{
    private void Awake() {
        PolygonCollider2D poly = GetComponent<PolygonCollider2D>();
        if(poly == null){
            poly = gameObject.AddComponent<PolygonCollider2D>();
        }
        Vector2[] points = poly.points;
        EdgeCollider2D edge = gameObject.AddComponent<EdgeCollider2D>();
        edge.points = points;
        Destroy(poly);
    }
}

This tutorial is sponsored by this community

In order to stick to our mission of keeping education free, our videos and the content of this website rely on the support of this community. If you have found value in anything we provide, and if you are able to, please consider contributing to our Patreon. If you can’t afford to financially support us, please be sure to like, comment and share our content — it is equally as important.

Join The Community

Discussion

Browse Tutorials About