Search Unity

How to force a VirtualCamera (transposer) to set its position instantly ?

Discussion in 'Cinemachine' started by Kiupe, Mar 8, 2018.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Hello guys,

    I have a Vcam that follows my player and it's working as expected. At some point in my game I "teleport" my player and the Vcam needs some times to reach the player and travel fast around the world. When I teleport the player I do not want the Vcam to follow and catch the player during seconds, I'd like it to "instantly" compute and set its final position and then get back to its "normal" behaviour.

    Is that possible ?

    Thanks.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    try vcam.PreviousStateIsValid = false;
     
  3. Go3k

    Go3k

    Joined:
    Oct 21, 2015
    Posts:
    17
    I have tried these two methods, but still can't update camera real position instantly, I want update camera's real position immediately not next frame, how can I make it? Thanks.

    1. disabling then re-enabling the vcam's gameObject.
    2. vcam.PreviousStateIsValid = false;
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Is your target animating on Update or FixedUpdate?
    When is the vcam updating? You can see it in the vcam inspector (next to the Solo button) when the game is running.
    When do you do vcam.PreviousStateIsValid = false ?
     
  5. Go3k

    Go3k

    Joined:
    Oct 21, 2015
    Posts:
    17
    1. Is your target animating on Update or FixedUpdate? The detail of my issue is:

    a. switch to a new scene, and there is a active vcam in this new scene.
    b. Init vcam, set transposer.m_FollowOffset and set vcam target position in Start event.
    c. then i have a get real camera position and do sth logic

    2. my vcam update in LateUpdate
    3. I dont know when should i set this property, so i tried before/after, in vcam Awake, but nothing happened.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    So, if I understand correctly, you are having the problem only at the beginning of the scene. You load a new scene which contains a vcam. In the Start() method of one of your scripts, you reposition the LookAt/Follow target of the vcam, assign the GameObject to the vcams LookAt and Follow, and set the vcam's m_FollowOffset. And the problem is that for one frame, the camera is in the wrong place. The next frame, it's fine.

    Is that correct?

    I don't understand what you're doing in your step c.
     
  7. Go3k

    Go3k

    Joined:
    Oct 21, 2015
    Posts:
    17
    Your understanding is mostly correct, As you said: " the problem is that for one frame, the camera is in the wrong place. The next frame, it's fine." Yes, this is the problem i want to fix.

    step c is I need the camera's position in that frame to continue my game logic, but since the camera is not in the right place, so my game logic went wrong.

    finally what i want is, in my initiate logic update the Camera(not the virtal camera) position instantly, not the next frame.

    Thanks for your reply. :)
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    So the problem isn't that the display is wrong, it's that you need the final camera position for the frame. I hadn't understood that before.

    The most reliable way to get this is to use CinemachineCore.CameraUpdatedEvent. It gets issued immediately after CinemachineBrain sets the camera position. Attach your game logic to that.
     
  9. Go3k

    Go3k

    Joined:
    Oct 21, 2015
    Posts:
    17
    OK, thanks.
     
  10. okk34

    okk34

    Joined:
    Aug 6, 2018
    Posts:
    4
    Hello! Trying to catch CameraUpdatedEvent, but receiving error. How properly setup "OnCameraUpdated function"? Please help. Cant find any examples in documentation.

    Argument 1: cannot convert from 'method group' to 'UnityAction<CinemachineBrain>' (CS1503) [Assembly-CSharp]


    private void Start() {
    brainEvent = CinemachineCore.CameraUpdatedEvent;
    brainEvent.AddListener(Ping)
    }

    private void OnCameraUpdated() {
    Debug.Log("completed!");
    /// my jobs here
    }
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Make it like this:
    private void OnCameraUpdated(CinemachineBrain brain)
     
    okk34 likes this.
  12. okk34

    okk34

    Joined:
    Aug 6, 2018
    Posts:
    4
    thanks for rapid reply! :)
    It compiles without errors now, but I'm receiving infinite amount of events
    (Debug.Log("completed!");) shows 999+ lines.

    Is it because of camera every frame changing position (cant see any changes in editor though) or because of wrong code logic?
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Camera position is updated every frame, even when it doesn't move. Is that the event you want to hook into? What do you need to be notified about?
     
  14. okk34

    okk34

    Joined:
    Aug 6, 2018
    Posts:
    4
    I have target group of objects. Sometimes weights of objects changes and I need to do some logic with UI after camera finished changing orthographic size. (need actual ortho size)
     
    Last edited: Sep 14, 2019
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    You won't get a specific notification that the ortho size changed. You'll just have to poll the value and keep track yourself.
     
    okk34 likes this.
  16. okk34

    okk34

    Joined:
    Aug 6, 2018
    Posts:
    4
    I thought as much. :( Thanks for confirmation.

    Since changing ortho size it is a process, not an instant event, hard to track final value.

    Easer way, but not so smart is Coroutine with 1 second delay. xD

    Anyway, thank you very much for your answers. :)
     
    Gregoryl likes this.