I want to place a bunch of rigidbodies inside a circle shaped collider. When I use the Circle Collider they are moved outside of the circle straight away.
Is there an option to allow them to stay within the wall?
Or do I need to resort to a polygon / edge collider? (in which case I need to draw the circle myself)
It sounds like the circle and the objects are both set to use the same layer, and the layers are set to collide with each other (configurable by the collision table in Edit->Project Settings->Physics 2D). Due to this they can’t exist in the same physical space, so the physics engine pushes them apart.
It sounds like you want the circle to actually ‘contain’ the objects, right? So they’ll sort of rattle around inside it? Try adding this script to a gameobject. It basically just creates an Edge Collider in a circle with the given radius and number of vertices.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent (typeof (Rigidbody2D), typeof(EdgeCollider2D))]
public class CircleEdgeCollider2D : MonoBehaviour
{
public float Radius = 1.0f;
public int NumPoints = 32;
EdgeCollider2D EdgeCollider;
float CurrentRadius = 0.0f;
/// <summary>
/// Start this instance.
/// </summary>
void Start ()
{
CreateCircle();
}
/// <summary>
/// Update this instance.
/// </summary>
void Update()
{
// If the radius or point count has changed, update the circle
if(NumPoints != EdgeCollider.pointCount || CurrentRadius != Radius)
{
CreateCircle();
}
}
/// <summary>
/// Creates the circle.
/// </summary>
void CreateCircle()
{
Vector2[] edgePoints = new Vector2[NumPoints + 1];
EdgeCollider = GetComponent<EdgeCollider2D>();
for(int loop = 0; loop <= NumPoints; loop++)
{
float angle = (Mathf.PI * 2.0f / NumPoints) * loop;
edgePoints[loop] = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)) * Radius;
}
EdgeCollider.points = edgePoints;
CurrentRadius = Radius;
}
}
Hey, I don’t mean to be a necromancer here but I’m looking to use this code to create a circle collider that is missing a piece ( to let the the object colliding with it to escape! I feel like there’s something i can edit here to make it have a missing piece?
extra love - if it could also be used to draw a line so the collider is on the inside of the line.