Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Confiner not working for 2D

Discussion in 'Cinemachine' started by xDayhawk, Sep 4, 2020.

  1. xDayhawk

    xDayhawk

    Joined:
    Jun 24, 2020
    Posts:
    55
    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:
    1. Created an empty object and added a Box collider - made it fit my entire level, its surrounding it.
    2. Added an Extension to Cinemachine called "Cinemachine Confiner":
      1. Confine mode is Confine 2D
      2. Bounding Shape is the collider i've created
      3. Confine screen edges it turned ON
      4. didnt touch Damping...
    upload_2020-9-4_23-39-12.png

    Green part is the Confiner of course:
    upload_2020-9-4_23-39-53.png

    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
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,153
    You need to take the warning seriously: confining shape must be a PolygonCollider2D or a CompositeCollider2D.
     
  3. xDayhawk

    xDayhawk

    Joined:
    Jun 24, 2020
    Posts:
    55
    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
     
    ApoxFox likes this.
  4. armaangera07

    armaangera07

    Joined:
    Jun 8, 2021
    Posts:
    1
    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 :)
     
    ApoxFox likes this.
  5. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    372
    Oh?

    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! :)
    This is kind of good news -- we can make more complex shapes with this anyways!

    upload_2021-9-6_14-25-31.png
     
  6. Develax

    Develax

    Joined:
    Nov 14, 2017
    Posts:
    67
    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?
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,153
    A polygon with 4 vertices is a box.
     
  8. SSKILLZSS

    SSKILLZSS

    Joined:
    Feb 23, 2022
    Posts:
    1
    upload_2022-3-21_23-34-50.png 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<PolygonCollider2D>();

    confiner.m_BoundingShape2D = BoundryShape;
    }

    }
    }
     
    Last edited: Mar 22, 2022
  9. fmoo

    fmoo

    Joined:
    May 31, 2015
    Posts:
    15
    Can we get support for BoxCollider2D added though? It's much easier to work with boxes in existing level creation tools than polygons.
     
    tbriz and Katsumo like this.
  10. Jim_Zee_King

    Jim_Zee_King

    Joined:
    Mar 10, 2014
    Posts:
    10
    You can use a boxcollider, you just have to change the geometry type of the composite collider to Polygons
     
    SnufkinGames and zeimhall like this.