Search Unity

Question Any drawback to update CinemachineBrain before game logic?

Discussion in 'Cinemachine' started by bitinn, Jan 17, 2022.

  1. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Hi,

    Usually in a game, we update game logic then camera, because we want to ensure changes to follow/look at target has been committed.

    But I recently encounter a scenario where we would like camera to update first: our movement logic and visual depend on a
    Camera.ScreenPointToRay
    from the center of screen.

    If camera update after game logic, then all visual will be 1 frame behind, when combine with player input like mouse position delta, it creates very ugly jitters. (because it is 1 frame behind a non-smooth input).



    The only reliable workaround I have found is to manual update CinemachineBrain before these logic, which stabilize visual.



    But I do worry whether this approach will be a problem down the road.

    Should I do this? Is there other ways?

    Thx!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Immediately after Cinemachine positions the camera, it fires an event:
    CinemachineCore.CameraUpdatedEvent
    . Can you hook into that event and perform your
    ScreenPointToRay
    logic there? Then you can let Cinemachine update in the normal fashion. The risk of updating manually before the targets move is that depending on the desired camera behaviour and the nature of target movement, you might get jitter (as you pointed out).
     
    Last edited: Jan 17, 2022
    gaborkb likes this.
  3. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Thx, I will try.

    And yes I do see jitter for the actual target being followed if I visualize it, but in this case I want to stablize the actual visual (ScreenPointToRay) more, so I made the compromise.

    (Perhaps I should follow a different target instead, but that require me feeding Cinemachine the follow target everytime.)