Search Unity

Smart camera framing

Discussion in 'Cinemachine' started by Eugenio, Sep 14, 2017.

  1. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    Hello forum !!

    So, what I'm trying to do is this: I have a scene with 2 objects in an arena and the camera is top down view.
    The camera is framing the center of the arena so that the border are on the borders.
    What I would like to do is having a smart camera that will decide to follow one of the object zooming in (in my case I'm moving the Z axis of the camera but I don't mind playing with the FOV) in a way that both of the object are still framed. The closer the object are, bigger is the zoom.
    Of course the max zoom has a limit as well as the min one.
    This is the tricky part: because I don't want the camera to follow the object so that the frustrum will go off the borders, what I would like to do is to stop following the object and zoom out to the fixed arena center view.

    Is it possible to do this with Cinemachine?

    Please note that the game will never stop while these transitions happens.

    Cheers !! :)
     
  2. JakubSmaga

    JakubSmaga

    Joined:
    Aug 5, 2015
    Posts:
    417
    Try Cinemachine Target Group.
     
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Use the 2D Camera from the Cinemachine menu, and have it follow a CinemachineTargetGroup. You can use a CinemachineConfiner extension on the vcam to prevent the camera from going past the boundaries. However, the CinemachineConfiner will only confine the camera point itself - the edges of the view are not considered. You would have to make a customized version of the confiner to implement this.
     
  4. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    Thanks for the replies guys!! :)

    I'll have a look and see what I can do.
     
  5. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    Sorry Gregoryl, but I cannot find 2DCamera and CinemachineConfiner in my project. I did download the Cinemachine package 2.0.1. Are those component present in this version?
     
  6. Adam_Myhill

    Adam_Myhill

    Joined:
    Dec 22, 2016
    Posts:
    342
  7. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    Cheers. I'll give it a go !! :)
     
  8. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    Hello again.
    So, I think that Cinemachine is not allowing me to do what I want if I use a TargetGroup.
    Please, let me explain better what I'm trying to do.
    There is an arena with a player object and an interactable object.

    Let's forget about the arena bouds for a moment.
    What I would like to achieve is to have the camera following the player object (so that the player object is always on the center of the screen) but adjusting the camera distance (or FOV) in such a way that the interactable will also be on screen. What Cinemachine is allowing me to do, is to frame both the object perfectly BUT in an average position (based on the weights I set in TargetGroup) in between the 2 objects. This means that I won't have the player object always in the middle.

    As I said before, let's forget about the arena constraints for now. My question is: can I do this simply using Cinemachine?

    Tahnks :)
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    @Eugenio Sorry for the long delay - I didn't see this post until now. You could do it with Cinemachine, but it requires a little custom script from you.
    1. Add your player to a target group, weight 1
    2. Add the other interactables to the group, weight 0
    3. Set your vcam to look at the target group, with the group composer
    4. Add a little script to the target group which, every Update, scans the group members and sets the player's radius (in the group) to the distance between the player and the most distant interactable
    That should do it. Hope it's not too late.
     
    Last edited: Sep 28, 2017
    CDF likes this.
  10. userexperiencesg

    userexperiencesg

    Joined:
    May 23, 2017
    Posts:
    5
    You are so crazy
     
  11. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    @Gregoryl

    Ehi, thanks for the reply !!!
    Actually I did solve by using the 2D components. It wasn't exactly what I wanted but I managed to have a satisfying result.
    Anyway, as soon as I will be back to that part of the project, I'll try also your solution !! ;-)

    Thanks a bunch again !!! :)
     
  12. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    This post saved my life
     
  13. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Actually I have another question.

    For a 3rd person camera... how can I, using the group composer, always ensure my player is on screen at a close range? Doesn't necessarily need to be centered. But always has to remain on screen without the camera moving back too far no matter what.

    I also want the group composer to attempt to look at other targets as well. But the player has to be on screen.

    What I'm finding is, I have to increase the max distance quite a bit, which is moving the camera out quite far away from the player in order to frame everything. Seems like the group composer is overriding the transposer. No matter what my transposer offset is, the group composer takes control. What I'd love is the group composer to just look at targets and not move
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    @CDF You'll have to give me a little context so I can understand what you're trying to do. I need to understand how the other targets are moving with respect to the player, and how you would like the camera to behave when the targets move too far apart to all fit on the screen. Do you want the camera to orbit around the player if the other targets go behind? What should happen if some are behind and some in front?

    What kind of vcam do you have on the player now?

    Yes the GroupComposer will try to get the entire group in the picture. It will adjust the camera's FOV and/or position in order to do that. You can set limits, however. If you set the min and max distance to be the same as the transposer offset, then the camera won't move, or better yet, tell it to only adjust the FOV in order to frame everybody. You can clamp that too, so that it won't be able to frame everybody if they're too far apart. What would you like it to do?
     
    Last edited: Oct 7, 2017
  15. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    I can't show screenshots, but might be able to make a video later with a test project.

    The camera's goal is to stay behind the player, looking in the direction the player is facing. Classic 3rd person camera.

    Player will encounter enemies, some static, some moving, some big, some small. These enemies, along with the player, are part of the Target groups target list.

    I'd like the camera to maintain a relatively close distance to the player, while also preserving the heading of the player and also attempt to look at close enemies. Or, enemies that *could* be framed without breaking the composition too much (staying close behind the player and maintaining heading).

    I totally understand the group composer tries to frame everything, even things behind the player and has limits. That's all good. I guess the issue I'm having are with these limits. I'm limiting the max distance so vcam stays close. But as a result, when framing other objects, the player gets lost because the group center (or average) is far beyond the max distance. So vcam is moving to that position, or attempting to frame it.

    Anyway, I think I've found a middle ground...

    Assign the player to the "follow" target. Assign group to "look" target. Set player weight and radius larger than all other targets. Blend out other targets weight based on some "max distance" variable. It's working fine, the camera does pull out a little more than I'd like too, but it's fine.

    I'm sure there's an elegant mathematical solution out there. Something like evaluating the framing shot of a target and adjusting its weight based on some priority value. But anyway. Thanks for the help.
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    I think you have the right approach. There's no magic "math bullet" for this, at least not one that I can think of.
    A script to maintain group membership and weights (setting to zero the weights of those members that are too far away or behind the player) is probably your best option. You can probably get the camera to not pull out at all if you get that script right.
     
  17. neubiole

    neubiole

    Joined:
    Oct 15, 2017
    Posts:
    14
    Informative! This thread is very interesting. I hope to learn more about cinemachine troubleshooting from this forum.