Search Unity

Camera functions like WorldToViewportPoint() and cinemachine

Discussion in 'Cinemachine' started by Hellwaiker, Feb 5, 2021.

  1. Hellwaiker

    Hellwaiker

    Joined:
    Jan 8, 2016
    Posts:
    118
    Hi,
    Basically what happens is I cannot grab the camera at the position when cinemachine has repositioned it.
    So, when I try for example to move UI to the world object position by converting the World/Screen positions. Like a tooltip code.

    I end up with a weird behavior where sometimes my calculations take camera position as it would have been without Cinemachine involvement.

    Probably because Cinemachine code executes in LateUpdate.

    Is there a way for me to get repositioned camera position reliably? So, I do my calculations after the cinemachine?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Yes, CinemachineBrain executes as late as possible in the pipeline, to ensure that it correctly tracks objects that are moving. You must take some steps to ensure that your code is called after CinemachineBrain has positioned the camera.

    There are 2 easy ways to do this:

    1. you can put your code in LateUpdate(), and set your script's execution order in Project Settings to happen after CinemachineBrain

    or

    2. you can hook into
    CinemachineCore.CameraUpdatedEvent
    , which is issued every frame immediately after the camera is positioned.