Confiner not working for 2D

Hi!

I’m a beginner in Unity, and I’m having difficulty confining my Cinemachine.

Its a pretty simple requirement, when i reach the end of the screen on the right or left, i dont want to the camera to keep moving.
additionally, i also dont want it to show some part of the bottom.

I did the following:

  • Created an empty object and added a Box collider - made it fit my entire level, its surrounding it.

  • Added an Extension to Cinemachine called “Cinemachine Confiner”:

  • Confine mode is Confine 2D

  • Bounding Shape is the collider i’ve created

  • Confine screen edges it turned ON

  • didnt touch Damping…

6279530--693941--upload_2020-9-4_23-39-12.png

Green part is the Confiner of course:

and it doesn’t work, it doesn’t confine anything although it sounds very stirght fowrad from what i’ve been reading…

Thanks in advance,

Dx

You need to take the warning seriously: confining shape must be a PolygonCollider2D or a CompositeCollider2D.

Thank you!

I actually saw that warning in the beginning, but when i used a polygon a lot of things didnt work, and the clamp didnt seem to work either, so i tried other Colliders and used the most basic one which is a box (since its a 2D platformer), and when that didnt work either i decided to use the forums… forgetting about that warning.

Using a polygon it does indeed clamp it to the borders i’ve made (a box), and i noticed something’s wrong with the physics so i just created another layer for the Confiner and took everything off from that new layer in the Physics 2D.
is that the correct approach if you have a collider within a collider? (my player is within the polygon, if its on the same layer).

Thanks again for the quick reply, and sorry lol

1 Like

I also had the same problem with the physics, and I never thought to add the confiner to the player layer. Thanks for helping out, I couldn’t find any video on this so this really helped me :slight_smile:

1 Like

Oh?

7476395--919216--upload_2021-9-6_14-22-54.png

Using the CinemachineConfiner2D component doesn’t give me that warning, so I was confused why it wasn’t working.
Thanks!

Using a PolygonCollider2D worked instead in my case as well! :slight_smile:
This is kind of good news – we can make more complex shapes with this anyways!

7476395--919220--upload_2021-9-6_14-25-31.png

I tired to use BoxCollider2D used by the CompositeCollider2D, but it still warns that
“CompositeCollider2D geometry type must be Polygone”.

I’m wondering why it must be exactly Polygon? I mean what is wrong with a rectangle shape? In my case, I need to change the confiner at runtime, and it seems to me to be more efficient to use simple forms like boxes, so why is a simple BoxCollider2D not supported and must have more than 4 vertices?

A polygon with 4 vertices is a box.

7983339--1025367--upload_2022-3-21_23-34-50.png7983339--1025370--upload_2022-3-21_23-35-45.png

I am trying to use my bounding shape Cam Boundry but the confiner won’t confine to it after I load a level and go back to the previous level. Any idea as to why it won’t confine? Could it be because the boundary isn’t in the DontDestroyOnLoad. How could I go about fixing this?

Fix:
I was able to write a script that got the gameobjects polygon collider component and use it for the confiner.

This is my code for people with similar issues.

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

public class DirectingCamBoundry : MonoBehaviour
{
private GameObject BoundryEmptyObject;
public PolygonCollider2D BoundryShape;

public CinemachineConfiner confiner;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (GameObject.FindGameObjectWithTag(“Boundry”) == null)
{
confiner.m_BoundingShape2D = null;
}

else
{
BoundryEmptyObject = GameObject.FindGameObjectWithTag(“Boundry”);
BoundryShape = BoundryEmptyObject.GetComponent();

confiner.m_BoundingShape2D = BoundryShape;
}

}
}

Can we get support for BoxCollider2D added though? It’s much easier to work with boxes in existing level creation tools than polygons.

2 Likes

You can use a boxcollider, you just have to change the geometry type of the composite collider to Polygons

3 Likes

If anyone comes looking for this as of Cinemachine 3.1.1 (maybe a bit earlier) you can just use a BoxCollider2D without using a composite collider on top.