Search Unity

Contain and zoom around a group of gameobjects in 2D

Discussion in 'Cinemachine' started by nicmarxp, Jan 9, 2020.

  1. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hi, I have an orthographic top down camera in 2d.

    How do I make the camera contain/frame a bunch of gameobjects, with a specific margin?

    I made a CinemachineTargetGroup, and added the gameobjects in it, but I can't figure out how to set it up so it zooms in enough so all are as big as possible.

    I noticed in the framing transposer, some new options turned up when I set "Follow" to the CinemachineTargetGroup, but it only seems to focus on the pivot of the gameobject, and I can't see how to make it contain it.

    Thank you in advance! :)

     
  2. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    Hi @nicmarxp !

    So I'd create your Target Group in three steps:
    1. Create a 2D camera (
      Cinemachine > Create 2D Camera
      ). This will set you up with a decent default 2D camera. For me, this is preferable to using
      Cinemachine > Create Target Group Camera
      simply because the defaults on a 2D camera are harder to tweak manually than the steps to set up a Target Group.
    2. Now we'll create the Target Group manually. Create an empty Game Object and name it something like "Target Group". In the Inspector, click "Add Component" and add a Cinemachine Target Group Component. Click the "plus" button and add the requisite number of slots for objects to include in the group. Now add your target GameObjects into those slots.
    3. Finally, click on your Virtual Camera. Assign your Target Group to both the Follow and Look At properties.
    That's it! The objects tracked by the Target Group will now automatically drive the camera to back off (or zoom in) to keep the targeted items in view.

    Hope that helps!
     
    apelsinex and nicmarxp like this.
  3. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Thanks a thousand, I'll try this! :D
     
  4. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    @marc_tanenbaum Hmm.. it seems like I get the same results as in my first post.
    Is there any specific requirements of the gameobjects in the Target Group?

    The structure is:
    LevelContainer (Empty gameobject)
    --LevelChunk 1 (Sprite shape renderer)
    --LevelChunk 2 (Sprite shape renderer)

    I tried different variants, to have the LevelContainer as targets, or each level chunk as targets, but no difference. It only focuses on the bottom left point, and doesn't change the ortho size at all.

    I don't know if it's cause of the Sprite shape renderers, but I managed to use render.bounds on the
    SpriteShapeRenderer to successfully get the bounds, so I'm assuming cinemachine should be able to do that too.

    Is there any specific setting on the vcam i should do? It's the default, which is set to Framing Transposer. I tried changing Group Framing Mode and Size, and also Min/Max orth size, but that doesn't do anything that helps get the level in the center of the view.

    Any ideas what could be wrong here or doesn't it work with Sprite shape renderer?

    Thanks!
     
  5. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    Hmmm...there are no specific requirements I'm aware of.

    I've put together a quick video showing how my recipe works, in case seeing it helps clarify where the problem is arising.



    I've also tried creating an empty "LevelContainer" GameObject and adding the targets (i.e., your "LevelChunk" objects) as children. I wouldn't expect that to make any difference and indeed in my tests it doesn't. To be clear, it should definitely be the LevelChunks...the things that change position...that are the targets, not the container.
     
    Last edited: Jan 10, 2020
    PeterJaschkowske and Gregoryl like this.
  6. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Thanks a lot for taking the time to make the video. When I do what you show with sprites, it works exactly like it should. But since I'm using Spriteshapes that I want to contain, it doesn't seem to work.

    Try the same thing with one or two sprite shapes. The camera doesn't to use the correct bounds of the sprite shapes.

    Should I place sprites in each corner of the sprite shape bounds, and then add them to the TargetGroup? I guess it would work, but seems a little hacky :)

    I think it's weird, since the sprite shape renderer has a bounds attribute/method (?) that gives the bounds just like a normal sprite renderer does.

    If you have the time to test with a sprite shape, and see if I'm correct or confused. :)

    EDIT: I might have discovered a bug. It works with sprite shapes, but after I edit the spine, it still uses the old bounds of the standard box shaped Sprite Shape. So maybe it doesn't recalculate the bounds after editing spline, or it's stuck to always use the bounds of the original sprite shape.
     
  7. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    Ah, I missed the detail that this was with Sprite Shapes, rather than just Sprites. And your edit seems to be correct...there seems to be some issue when editing splines. Let me check with the relevant teams and get some input on what might be going on here.

    Note that Sprite Shapes are currently listed as in "preview", which basically means that they're not guaranteed to be production ready.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Note that there is no logic in CinemachineTargetGroup to determine the size of the target. You have to set that manually.

    upload_2020-1-10_14-44-2.png
     
  9. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Aha! That (maybe) changes everything, and I could input the values from the SpriteShapeRenderer.bounds here, using a script.

    There still might be a problem since the anchor/center of the sprite shape is in the bottom left according to my first image..
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Maybe you could use an invisible intermediate object that tracks the actual target, positioning and sizing itself appropriately for inclusion in the group.
     
  11. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Thanks, yes I will do that instead. :) Thank your for bringing the attention to the teams about the bounds of the spriteshape maybe not updating correctly.