Search Unity

I want to setup my camera with seperate screens like how they used in Celeste

Discussion in 'Cinemachine' started by NoctisShadowzel, Jul 27, 2019.

  1. NoctisShadowzel

    NoctisShadowzel

    Joined:
    Dec 27, 2018
    Posts:
    57
    Here is a media that shows what I mean with screens;


    To reach collecticable, you need to use screens and how it affects game mechanics.
    In my game I have 4 seperate 512x256 screens at my starting screen. I want to implement this, but I only used Cinemachines Follow & Confiner 2D functionalities before. Is there a guideline I can follow, or an example I can investigate?

    PS. As a side-question (I'm not sure if this is a existing term), what does Cinemachines LookAt field used for? Is it for following targets rotation?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Sorry for being thick, but I still don't understand what you mean by "screens". Can you describe a little more precisely?

    A virtual camera can be positioned in space, and it can be rotated to look at something. These can be independent, and driven procedurally based on 2 different targets: the Follow target for the position, and the LookAt target, for the aim. In a 2D game, you probably don't want to ever change the aim, so you will only need a Follow Target, with a Framing Transposer in the Body section of the vcam.
     
  3. NoctisShadowzel

    NoctisShadowzel

    Joined:
    Dec 27, 2018
    Posts:
    57
    No problem, here is what I mean by screens. (Before explaining, I didn't give name "screens" to this, it is described as seperate screens in game's fandom. To reach the top, you need to dash between left and right screen.)

    I attached a Paint-made image :)P), think scene is made of this squares. If player is in first square, camera is in first square. When player moves, camera moves to next screen, and (in Celeste, I don't want to implement this) characters physics mechanics reset. This could be done with a few position checks, but my game is growing. So I want to know if there is a Cinemachine-specific way to implement this feature.

    -TY for reply ^^
     

    Attached Files:

  4. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    Screens are also referred to as rooms in a 2D context.
    I got this working pretty well in Cinemachine with triggers, basically a box collider per room. On contact with the player, the virtual cam linked to that box activates and all the others deactivate, creating the blend or pan between the rooms.

    A major issue I have though, that I have not found a solution for yet, is when the player returns to the previous room before the end of the Cinemachine blend transition, even if as short as 0.1s, the Vcam fails to activate and come back to the player. In other words, the main cam is showing the wrong room.
     
    Gregoryl likes this.
  5. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    Here's a screenshot:
    Every room is basically a Box Collider Trigger, which also serves a a confiner in bigger rooms.
    Every room contains a VCam.
    OnTrigger with the player, the Vcam of that Room activates, and deactivates the other Vcams, leading to a blend, which you can control on your Main Camera Cinemachine Brain.
     

    Attached Files:

    • cine.png
      cine.png
      File size:
      55.3 KB
      Views:
      935
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Yes, and you can attach the CinemachinTriggerAction script to the trigger, to activate the vcam on player enter, and deactivate it on player exit, all without custom code. If the triggers overlap a little, you will get nice hysteresis-prevention.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    This should work properly, the vcam should not fail to activate. How have you implemented the activation?
     
  8. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    The vcam activates properly. But lets say if the player jumps back to the previous room before the blend has finished between both vcams, that is where the problems arise.

    Here is an extract from the code, which every "Room" contains = BoxCollider2D Trigger & Vcam
    (the Coroutine is overkill, but was meant as a plug for this issue, but did not help solve the problem)


    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.gameObject.layer == layerMask)
    4.             StartCoroutine(PrepareBlendCo(collision.transform));
    5.     }
    6.  
    7.     IEnumerator PrepareBlendCo(Transform player)
    8.     {
    9.         while (cinemachineBrain.IsBlending)
    10.             yield return null;
    11.  
    12.         SwitchRoom(player);
    13.     }
    14.  
    15.     private void SwitchRoom(Transform player)
    16.     {
    17.         DeactivateCam();
    18.  
    19.         if (myVirtualCamera.Follow == null)
    20.             myVirtualCamera.Follow = player;
    21.  
    22.         myVirtualCamera.gameObject.SetActive(true);
    23.         SetCheckPoint(player);
    24.     }
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    You should be able to re-activate the outgoing vcam, even if it's still in a blend. The blends chain. So the pattern:

    if (enteredRoom)
    {
    DeactivateCurrentVcam()
    ActivateVcamForThisRoom()
    }

    Should always work, regardless of blend state. No need for coroutines, or waiting for blend to finish. No problem to deactivate a vcam while it's blending, the brain will keep it on artificial life-support until the blend is done. Just don't delete it or recycle it.
     
  10. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    Thanks.
    Got rid of the temp coroutine and even added a myVirtualCamera.MoveToTopOfPrioritySubqueue(); on activation, but still the same result.
    I'll post a short video in the morning. Somehow I can generate that problem on every play, so I'm obviously going down the wrong lane.


    ps.: sorry to NoctisShadowzel for hijacking this post. Hopefully all this info will be beneficial for setting up a Celeste style camera management system.
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    If you could put together a minimal project that shows this problem and post it here (or PM it to me) that would help a lot
     
  12. NoctisShadowzel

    NoctisShadowzel

    Joined:
    Dec 27, 2018
    Posts:
    57
    No need to sorry, I am glad it is useful for someone else too!
    Also thanks for answers.
    @Gregoryl, I want to ask a second question. I always use a empty game object with only a polygon collider which points are almost exactly on backgrounds corners. For this game, I used GameObjects with EventTriggers to make a UI, because it is going to be interacting with Main Character, and it became a problem when I noticed screen-sized polygon collider is messing up my touch receivers. I disabled GO which contains polygon coll. and tested if 2D Confiner still works. With the Bounding Box disabled VCam still stays in bounds while following my character, but it behaves like screens I asked in my first question. It doesn't do smooth transition. Even if this is what I wanted to achieve, I wanted to ask you if this is expected behaviour... (I didn't do any other changes to my scene with VCam.)
     

    Attached Files:

  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    You're saying that confiner respects the bounding box whether or not the bounding box GameObject is active? If so, yes that's correct. If you want to turn off the confiner you can disable the confiner extension.
     
    Last edited: Jul 30, 2019
  14. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    I made a short video which demonstrates the issue, and even the cause, but I do not know how to solve it.


    As you can see, the player enters the confiner collider of the adjacent room, and triggers the activation of the vcam of that room, and the deactivation of the current one. The blend works fine.

    The problem is when the player does not fully exit the confiner collider/trigger of the previous room, but returns to it, failing to cause a new trigger. Also causing the player to respawn in the wrong room after death.

    One solution would be to not use the confiner colliders as blend triggers, but make dedicated trigger with ample space in between triggers so that the player always exits these. Not a very clean solution, since depending on the output (pixel perfect 2:1, 3:1, 4:1 etc), the player might have to blindly run out of the display for a certain amount of time before hitting the next trigger, which is not a positive gaming experience.

    There might be a better way to do this?
    Thank you.
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Are you moving the collider? Don't do that. Have one collider per room, and this problem will go away.
     
  16. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    Every room has its own static collider. What you see moving in the clip is the Pixel Perfect gizmo.
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Do the colliders overlap a little? If so, then you can add an OnTriggerExit handler to disable the room's vcam. That should solve the problem. If you enter the overlapping zone, new vcam will activate. Old vcam will remain on standby until you leave the overlapping zone. If you go back where you came from then new vcam will deactivate, and old vcam will become live again.
     
  18. NoctisShadowzel

    NoctisShadowzel

    Joined:
    Dec 27, 2018
    Posts:
    57
    Oh, nope, I think I couldn't adress my question right way. What I'm asking is, when Bounding Box is enabled, VCam follows the main character, as it should. But when Bounding Box is disabled, the effect I opened this thread for, screens/rooms, happening without any additional changes. I just didn't understand why did this effect came in play when I disable the bounding box, it seems weird. That is what I asked if expected behaviour or not.
    Since this thread goes on, I will put a video of my development to show this weird behaviour I'm asking about; if question isn't clear.
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Maybe a video will help. Unfortunately I don't understand what you're asking.
     
  20. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    Yes they do, so your solution worked like a charm! Wonderful, many thanks.
     
    Gregoryl likes this.
  21. NoctisShadowzel

    NoctisShadowzel

    Joined:
    Dec 27, 2018
    Posts:
    57
    Even if I'm going to change my Camera handling to the method @zapposh have suggested, I'm sharing a video to show the problem I'm seeing with my current Camera/Cinemachine setup.

    First of all, as you can see in video, when I disable gameobject with collider I use as Bounding Box, Cinemachine don't do smooth transition anymore. It behaves like if I implemented screens/rooms (that was topic of this thread), it changes position if only I pass from a position, an edge of Cameras viewport. That was what I was asking, if expected behaviour or not.
    Secondly, at between middle/up and right room, Camera is showing a not related room, that is under the player.

    There is only one Polygon Collider, shaped as same as scenes shape, three equal rectangles at top; one at middle/bottom - like a T, only one VCam, one Main Camera.

    Even if I'm changing my approach, I wanted to ask if this is a bug, or is it about my setup. (I also tried to play with settings at Body section with Framing Transposer selected, nothing worked.)
     
  22. NoctisShadowzel

    NoctisShadowzel

    Joined:
    Dec 27, 2018
    Posts:
    57
    @Gregoryl, I'm not using Physics, so my Collision Check is based on Raycast, I tried to use CinemachineTriggerAction script with VCams, also added a CinemachineCollider too. Didn't worked. Also tried with Trigger BoxColl2D, but I think, I need Physics collisions for this to work as expected. I think I must do position check from script...
     
  23. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Yes the CinemachineTriggerAction script uses physics raycasts.

    For you video, it seems very strange. Without the collider, your virtual camera with the framing transposer should follow your character smoothly, and completely ignore the rooms.

    Do you have some other script that's affecting the camera? Are you perhaps not following the player directly, but some other target that you're position with other logic?
     
  24. NoctisShadowzel

    NoctisShadowzel

    Joined:
    Dec 27, 2018
    Posts:
    57
    @Gregoryl, sorry about not responding quickly. Been thinking about a new idea...
    No, there isn't.

    Because I changed the way I implemented this Camera setup, I don't have the setup I have in video right now. But to answer your question,
    My Player is child of a empty game object, that doesn't change position (Parent is always at Vector3.zero). But VCam's Follow field was set to Player, not to parent GO.
     
  25. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Without more context, I can't tell you why the camera is jumping suddenly from room to room. There is no logic in Cinemachine that would generate this behaviour. From what I can tell based on the inspectors I was able to see in your video, the camera should just follow smoothly and ignore the rooms. I can only conclude that there is something else in your setup that's making this happen.
     
  26. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    Silly question, but do you have a blend time on the blend property of the CinemachineBrain on the camera? Without it you'll have cuts instead of blends.
     
  27. NoctisShadowzel

    NoctisShadowzel

    Joined:
    Dec 27, 2018
    Posts:
    57
    In the video I posted, there is only one Virtual Cam and one GO with Polygon Collider shaped as same as level itself, used as Bounding Box.
    So there is no blend, no rooms or anything. I posted that video to ask @Gregoryl if this is a bug or not.
     
  28. BenBonkGames

    BenBonkGames

    Joined:
    Jul 13, 2018
    Posts:
    16
    Hey, so if anyone found this old thread from a google search, like how did and couldn't figure out the solution, well I finally figured it out and made a simple tutorial on it -
    Let me know if this helps anyone out.
     
    Dev_A and keith801 like this.
  29. keith801

    keith801

    Joined:
    Jan 9, 2019
    Posts:
    8
    Thanks for sharing this, it's really helpful for what I'm trying to do.
     
  30. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Nice tutorial, thanks for that.

    Note that you can use the handy CinemachineTriggerAction helper script, then you don't even need to write a custom script to trigger the cameras.
     
    BenBonkGames likes this.
  31. keith801

    keith801

    Joined:
    Jan 9, 2019
    Posts:
    8
    Any good tutorials on how to use the trigger action? Or examples?
     
  32. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    Look at the Sample scene shipped with Cinemachine. A number of them use CinemachineTriggerAction.
     
    keith801 likes this.