Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cinemachine Virtual Camera Switch target at runtime in virtual camera, its causing jerk?

Discussion in 'Cinemachine' started by manish191, Oct 3, 2017.

  1. manish191

    manish191

    Joined:
    Jul 6, 2017
    Posts:
    9
    Hi, @Adam_Myhill, I am using cinemachine virtual camera for my racing game. And I am using multiple virtual cameras, to show different views in racing. But I am facing one issue with virtual camera. Its when I am assign a new target from previous target to the virtual camera its suddenly changing the camera position. So its looking like a jerk. Can we have smooth movement when changing from previous target to new target in virtual camera?

    It would be great if you help me on this. Thanks in advance
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    @manish191 It is by design that a target switch on a vcam acts as a cut, not as a blend. The official way, if you want a nice smooth blend, is to have a second vcam on the new target, enable it (or boost its priority), and let the Cinemachine blender make the transition.

    Do you have a special reason why you need to make this transition using a single vcam?
     
    Harinezumi likes this.
  3. manish191

    manish191

    Joined:
    Jul 6, 2017
    Posts:
    9
    Hi, Gregory, Thanks for the response. Actually, I am using Cinemachine for a racing game. Where Player will be running and Also I am using multiple VCam, But the problem is target switching is very frequent in racing. Right Now I am using 6 Vcam to cover full race track. Also If I Use more VCam in game is there any performance issue? suggestion please
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    You can have as many vcams as you like. There is a small cost for each active one, as it will be following its target, but it's not rendering or anything like that. You can eliminate even that cost by disabling the vcams, and only enabling them when you switch to them.
     
  5. manish191

    manish191

    Joined:
    Jul 6, 2017
    Posts:
    9
    Thanks, Gregory for the response. I will Make sure to enable and disable according to there requirement in game. I will make the change lets see how it will work. Thanks
     
  6. Sutee9

    Sutee9

    Joined:
    Mar 12, 2015
    Posts:
    6
    Cool, exactly the reply I was looking for. Avoids a more complicated path animation!
     
    Gregoryl likes this.
  7. ertsnom99

    ertsnom99

    Joined:
    Dec 5, 2016
    Posts:
    2
    I'm having a similare problem where, in my case, I would like to switch from a virtual camera to an other, but have a transition that follows along a Dolly Track. I'm not sure if I can actually do that, but the reason I want to use a Dolly Track is that my camera is going to move around in a mansion and I would like it to go throw some specific corridors (follow the Dolly Track). The other reason I want to use a Dolly Track is that I don't want my camera to go throw walls.

    If it's not really possible, maybe have one virtual camera on a dolly and changing the follow and look at, but still have it make a transition along the track (I tried just changing the follow and look at by code, but it just cut instead of doing a transition)?
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    Interesting. You want to transition from camera A to camera B, but you want that transition to follow a dolly track.
    What are camera A and camera B doing? Are they also on tracks? Are they FreeLooks, or follow cams, or something like that?
    What do you want the camera to be looking at while it's transitioning back to camera A?

    I can offer this idea, which may or may not be appropriate (I would need more info about your cameras):

    Suppose you make camera C, which is a dolly along the transition track. Set its follow target to be an invisible gameObject, which you will manually lerp from camera A's position to camera B's position - right through walls etc). When it's time to transition, you do this:
    1. Activate camera C (CM will transition from A to C)
    2. Lerp the invisible object from A to B (CM will cause camera C to seek the closest point on the track to the invisible object, but it will stay on the track)
    3. When the lerp is finished, activate camera B (CM will transition from C to B)
     
    mannyhams likes this.
  9. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    Gregory I am having the same need as Suree 9. I will try your suggestion of lerping an invisible target. But it seems like such a great omission on the track and dollly cam.

    The use case is a cutscene. We would like the camera to follow a path as we explore the scene but also target different points of interest as it follows its path. Seems pretty standard.

    Isn’t the track and dolly the correct camera to use.
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    Yes, use the track and dolly. You don't need an invisible object - just animate the vcam's position on the track.

    upload_2020-2-10_9-48-0.png

    You can make multiple such vcams on the same track, in the same position, moving at the same speed, but with different LookAt targets. At the appropriate times (maybe with TriggerVolumes - look at CinemachineTriggerAction for a nice helper script) adjust the priorities of the vcams to transition smoothly between them
     
    hawaiian_lasagne likes this.
  11. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    I had the same requirement as @ertsnom99 to vary lookat targets along a dolly track. I ended up with a timeline that has an animation track for each camera, animating the path position identically. And a cinemachine track to switch between the cameras. It works, but a little clunky. Would be nice to have a single camera a allow smooth blending between lookat targets
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    @mashyo the easiest way to do that would be to have an invisible object which is the actual LookAt target, and animate it from one place to the next.

    But we take your point: it would be nice to have a way to smoothly change targets in a single vcam. Thanks for the suggestion.
     
  13. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    @Gregoryl Ah I see what you mean, thanks that is a lot cleaner :). I now only have a single camera, and a single lookat target, rather than 3 cameras and 3 lookAtTargets.

    And, my timeline went from this:
    upload_2020-3-19_14-33-34.png

    to this:
    upload_2020-3-19_14-33-57.png

    Cheers!
     
    Gregoryl likes this.
  14. K0ST4S

    K0ST4S

    Joined:
    Feb 2, 2017
    Posts:
    35
    One camera is much simpler and cleaner in code, would be nice if we could choose the speed of camera transition after follow target changes! :/
     
    _eternal likes this.
  15. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    302
    Seconding this, as it can lead to messier code for some use cases to have multiple virtual cameras. In 2D, I only really need one camera, but I regularly change its orthographic size. If I want to change follow targets at runtime, I guess I need other virtual cameras for the other targets, but I'll have to manually mirror the same ortho size to all of them.

    It's doable, but perhaps more awkward than it needs to be, since all I'm trying to do is pan during a cutscene or with the player's analog input, and then return to following the player afterward. But when I re-set the player as the follow target or re-enable the Cinemachine brain, it jumps straight back instead of smoothly returning to default.
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    Transitioning from a non-CM camera to a CM-driven camera will not lerp the camera to the desired position. For the cameras that you want to control directly, use a CM vcam instead of a Camera component. While that vcam is active it will fully control the main camera. If you set it up that way, you have the possibility of using CM blending, even to/from vcams that are 100% driven by your custom code.

    Your cutscene can blend to/from the player cam without deactivating the Brain or changing the target of the player cam.
     
    _eternal likes this.
  17. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    302
    Thanks. Yeah, it feels funny at first, but ultimately I guess it's better to use multiple virtual cameras, and just set them to Do Nothing if you want to control some of them by code. My player input camera is more or less working now.
     
    Gregoryl likes this.
  18. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    Yes, exactly.
     
  19. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    322
    +1 for this!
    I have a Dolly and I do want to change the Target between some points to be able to get some more interesting angles on some path positions. I have done a code to make this but I got "direct cuts" instead of transitions. Is this still not possible?
     
  20. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    The easiest way to do this is to use an invisible object as target and change its position
     
  21. adrianov

    adrianov

    Joined:
    Dec 23, 2015
    Posts:
    15
    Hi @Gregoryl, I'm attempting to switch between two cameras with different targets but am having unexpected issues. I'm following your suggestion above of having multiple vcams with separate targets and let cm do the blending between them, but cm is not blending smoothly between them, in terms of looking at the targets; when blending between cameras it smoothly moves position but doesn't start to move it's aim until the very last moment, when it just snaps to the new vcam's lookat target.

    Here are my two vcams: Screen Shot 2022-04-28 at 10.29.21 AM.png Screen Shot 2022-04-28 at 10.30.00 AM.png
     
  22. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    It would be good to have a video of the problem.

    From your description, it could be that one camera is looking straight at a target at point A in front of it, and the other camera is looking at a target at point B that directly behind the first camera. As the target position lerps from point A to point B, no rotation is necessary until the lerped point passes the first camera, and then it will have to turn very suddenly.

    You could try changing the blend hint to CylindricalPosition. Maybe that will help.
     
  23. adrianov

    adrianov

    Joined:
    Dec 23, 2015
    Posts:
    15
    @Gregoryl so changing the blend hint to Screen Space Aim When Targets Differ helped a little, but it still doesn't look great.
     
    Last edited: Apr 28, 2022
  24. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    Second target is the red guy. What is the first target? How far away is it?
     
  25. adrianov

    adrianov

    Joined:
    Dec 23, 2015
    Posts:
    15
    I guess what I'm hoping for is to have the camera attempt to place the character in the middle of the screen as it moves down.
     
  26. adrianov

    adrianov

    Joined:
    Dec 23, 2015
    Posts:
    15
    First target is a point in the far distance. Just a transform. Could be any amount of distance away, it gets placed dynamically right now but could be placed "by hand". Represents the end point of the level and the idea is the camera looks across the landscape toward that point to look at the entire level.
     
  27. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    What happens if you move that far point much closer?
     
  28. adrianov

    adrianov

    Joined:
    Dec 23, 2015
    Posts:
    15
    It kinda helps, but not that much, plus it totally changes my framing If I do that so kinda defeats the purpose...
     
  29. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    If you're willing to send me a quickie test project with this blend in it, I can have a look at it. It's hard for me to know exactly what's going on otherwise.
     
  30. adrianov

    adrianov

    Joined:
    Dec 23, 2015
    Posts:
    15
    Unfortunately, it's for a client and I don't believe I'm allowed to share the project. Let me see If I can quickly get a test project I can send you though.
     
  31. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    yeah, just do a mock-up
     
  32. adrianov

    adrianov

    Joined:
    Dec 23, 2015
    Posts:
    15
    This should be representative of that camera blend.
     

    Attached Files:

  33. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,624
    Sorry for the delayed response, I was dragged away to other things.
    Thanks for the upload.

    The problem comes from the fact that the initial lookAt target is so far away. Over the course of the blend, CM is interpolating the LookAt target from point A to point B, and since that distance is over 1km and since the camera is so close to point B, it just can't slow down enough at the end to make it smooth. It's a problem of scale.

    There are a few ways to fix this:

    A. Make a custom blend curve for this blend that gets really slow at the end (hard to do with precision), like this:
    upload_2022-5-4_16-44-1.png

    B. Move the distant target much closer to the camera, along the line between the target and the camera. This will not change the framing. If you don't want to move the target itself, you can use an intermediary invisible object as a target, placed along the line between camera and real target.

    C. Don't use a LookAt target in the initial shot. Just position the vcam the way you want it, with DoNothing in Aim and Body, and no LookAt or Follow targets. That in my mind is the simplest solution, and gives a lovely smooth blend.
     
    akent99 likes this.
  34. adrianov

    adrianov

    Joined:
    Dec 23, 2015
    Posts:
    15
    Thanks for the reply! Those all sound like solid options. The problem is no longer relevant at the moment, but having these possible solutions for it in my pocket will certainly come in handy in a near future, Thanks again!