- Home /
Cinemachine confiner on different aspect ratios
I have a basic Cinemachine camera setup which follows the player and I added a confiner to limit the camera's position. The bounding area for the confiner is just polygon collider formed as a square on the background.
I only support landscape mode and started off with a 16:9 aspect ratio, where everything is fine. My problem is when the aspect ratio changes to for example 2:1. Then the camera becomes a bit wider than my defined bounding/confined area and the confiner pretty much breaks.
This is how it is supposed to work: https://imgur.com/a/I5DAHtn
And this is the result of a wider resolution: https://imgur.com/a/tzIAahx
(The yellow border is the collider being used as a confiner and the white is the camera).
How do I get around this problem? Can you dynamically change the confiner to never be smaller then the camera view? Or do I have to adjust the camera size somehow? Currently using an orthographic camera with a size of 5.
Not sure what to do here, any help is appreciated!
Answer by kunf · Apr 16, 2020 at 06:14 PM
One of solutions - add script to colliders used as confiner boundaries. In my case - my colliders are rectangle and initially used for 16:9 aspect. For 16:10 i need to make colliders 3.2 units wider, if they have width. And keep zero width for any colliders with zero width no matter which aspect ratio is used.
void Awake()
{
if(Camera.main.aspect == 16.0f/10.0f)
{
var allPoints = GetComponent<PolygonCollider2D>().points;
for (int i = 0; i < allPoints.Length; i++)
{
if (allPoints[i].x < 0)
{
allPoints[i].x -= 1.6f;
} else
{
if (allPoints[i].x > 0)
allPoints[i].x += 1.6f;
}
}
GetComponent<PolygonCollider2D>().points = allPoints;
}
}
And more flexible version for supporting any aspect ratio:
float orthoCameraSize = 9.0f;
void Awake()
{
float aspectFix = Camera.main.aspect * orthoCameraSize;
Vector2[] allPoints = GetComponent<PolygonCollider2D>().points;
for (int i = 0; i < allPoints.Length; i++)
{
allPoints[i].y = ($$anonymous$$ath.Abs(allPoints[i].y) - orthoCameraSize) * $$anonymous$$ath.Sign(allPoints[i].y);
allPoints[i].x = ($$anonymous$$ath.Abs(allPoints[i].x) - aspectFix) * $$anonymous$$ath.Sign(allPoints[i].x);
}
GetComponent<PolygonCollider2D>().points = allPoints;
}
I made my colliders exact size of room. And write orthographic camera size in script ins$$anonymous$$d of reading from camera, because using perspective camera. Script shrinking my rectangle colliders depending on aspect, so camera will always show only contents bounded by collider.