I'm using this code from the UnityCommunity Github to create edge colliders around the border of the screen:
public class ScreenEdgeColliders : MonoBehaviour
{
private Camera cam;
private EdgeCollider2D edge;
private Vector2[] edgePoints;
void Awake()
{
if (Camera.main == null) Debug.LogError("Camera.main not found, failed to create edge colliders");
else cam = Camera.main;
if (!cam.orthographic) Debug.LogError("Camera.main is not Orthographic, failed to create edge colliders");
// add or use existing EdgeCollider2D
edge = GetComponent<EdgeCollider2D>() == null ? gameObject.AddComponent<EdgeCollider2D>() : GetComponent<EdgeCollider2D>();
edgePoints = new Vector2[5];
AddCollider();
}
//Use this if you're okay with using the global fields and code in Awake() (more efficient)
//You can just ignore/delete StandaloneAddCollider() if thats the case
void AddCollider()
{
//Vector2's for the corners of the screen
Vector2 bottomLeft = cam.ScreenToWorldPoint(new Vector3(0, 0, cam.nearClipPlane));
Vector2 topRight = cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, cam.nearClipPlane));
Vector2 topLeft = new Vector2(bottomLeft.x, topRight.y);
Vector2 bottomRight = new Vector2(topRight.x, bottomLeft.y);
//Update Vector2 array for edge collider
edgePoints[0] = bottomLeft;
edgePoints[1] = topLeft;
edgePoints[2] = topRight;
edgePoints[3] = bottomRight;
edgePoints[4] = bottomLeft;
edge.points = edgePoints;
}
}
I attach this script to my main camera. It works pretty well. Objects are contained and generally bounce off the walls. The problem is that objects sometimes get stuck in these colliders and I can't drag them away from the walls. Is there anything I can add to this code to make sure that objects don't get stuck?
The issue is that you are creating an very thin collider (infinitely thin), and your object colliders are going through the edge collider and getting stuck on the other side.
There are two ways to solve this:
The easy way is to set your collision detection on your Rigidbody2D to "Continuous", setting the Interpolation to something other than "None" could also help.
However if for whatever reason your objects are being moved via "transform" and not physics. (Which shouldn't be the case) you might just want to implement a thicker collider.
A thicker collider might be safer either way, especially if (for performance reasons) you still want to keep the collision detection to "Discrete" and Interpolation to "None"
If that is the case, try replacing your "AddCollider()" function with the following one.
//Use this if you're okay with using the global fields and code in Awake() (more efficient)
//You can just ignore/delete StandaloneAddCollider() if thats the case
void AddCollider()
{
//Vector2's for the corners of the screen
Vector2 bottomLeft = cam.ScreenToWorldPoint(new Vector3(0, 0, cam.nearClipPlane));
Vector2 topRight = cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, cam.nearClipPlane));
Vector2 topLeft = new Vector2(bottomLeft.x, topRight.y);
Vector2 bottomRight = new Vector2(topRight.x, bottomLeft.y);
float thickness = 5;
//Update Vector2 array for edge collider
edgePoints[0] = bottomLeft + new Vector2(-thickness, -thickness);
edgePoints[1] = topLeft + new Vector2(-thickness, thickness);
edgePoints[2] = topRight + new Vector2(thickness, thickness);
edgePoints[3] = bottomRight + new Vector2(thickness, -thickness);
edgePoints[4] = bottomLeft + new Vector2(-thickness, -thickness);
/// Add thickness to actual 2d collider
edge.edgeRadius = thickness;
edge.points = edgePoints;
}