Search Unity

Cinemachine Confiner for 2D - to block at the borders of the map

Discussion in 'Cinemachine' started by Jesus_Had_Aegis, Nov 29, 2017.

  1. Jesus_Had_Aegis

    Jesus_Had_Aegis

    Joined:
    Oct 7, 2017
    Posts:
    3
    To start with I am beginner game dev, and new to using Cinemachine. I am trying to restrict the camera exposing the edge of the screen. I know that i have to add a Cinemachine Confiner and to have a collider. But my map is generating procedural and so I will have to add the collider programmatically and also somehow tie the collider to the Cinemachine Confiner and I don't know how, or couldn't find the necessary documentation..

    What I've done until now:
    - I have created a collider
    - I have created a Cinemachine.Cinemachine confiner
    - I have created a CM vcam1 in the hierarchy

    How to tie them together?


    Thanks in advance
     
    bowserscastle likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,718
    Your collider is a PolygonCollider2D, right?
    If you're using the TileMap, you can have it generate a CompositeCollider2D programmatically, and you can use that.
    Once you have the collider, just populate the fields of the CinemachineConfiner component:

    upload_2017-11-29_15-14-7.png

    From script, you can do it as follows:
    Code (CSharp):
    1. using Cinemachine;
    2. confiner.m_ConfineMode = CinemachineConfiner.Mode.Confine2D;
    3. confiner.m_BoundingShape2D = myPolygonCollider;
    4. confiner.m_ConfineScreenEdges = true;
     
    Last edited: Nov 29, 2017
    bowserscastle likes this.
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,718
    I forgot to ask you: how did you create the vcam and the CinemachineConfiner? Can you show me a picture of the inspector for your vcam?

    It should look something like this:
    upload_2017-11-29_15-20-0.png
     
  4. Jesus_Had_Aegis

    Jesus_Had_Aegis

    Joined:
    Oct 7, 2017
    Posts:
    3
    Thanks for the quick answer, I am now looking into ways of creating a polygon collider 2D, cause mine was a Box Collider 2D. The camera looks exactly like that.
     

    Attached Files:

    meatCereal likes this.
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,718
    I made a mistake in my earlier post: the TileMap can generate a CompositeCollider2D, not a PolygonCollider2D. This generated shape can be used as the bounding shape.
     
  6. Jesus_Had_Aegis

    Jesus_Had_Aegis

    Joined:
    Oct 7, 2017
    Posts:
    3
    Thanks a lot! If anyone is intrested I made it by creating a Composite Collider, enabling isTrigger, then adding my boxCollider2D as a child of the said Composite Collider, with a tick at "Used by Composite" and then editing the boxCollider2D size and offset from the script.