Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Teleport camera on Character death

Discussion in 'Cinemachine' started by bconstantin, Jul 2, 2019.

  1. bconstantin

    bconstantin

    Joined:
    Feb 4, 2019
    Posts:
    15
    Hello,

    I am trying to teleport the camera to check point after character die in my game. But I can't do it with cinemachine for the moment. Someone know how to teleport Camera to a certain point ? without lerping the entire level ?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,526
    While the vcam is procedurally tracking, it will control its own transform and overwrite whatever you do.

    If you warp your target and want the vcam to warp also, you can set vcam.PreviousStateIsValid = false to stop it from damping.
     
    bconstantin likes this.
  3. Arkasis

    Arkasis

    Joined:
    Nov 22, 2010
    Posts:
    59
    Hello, I'm trying to teleport my "fraing transposeur" camera which is following a target in a 2D game to a specific location without any damping.

    I use this code :

    Code (CSharp):
    1.        
    2. // Set target position and camera position
    3. m_CameraTarget.position = m_FollowedPlayer.transform.position;
    4. m_CineVirtualCamera.transform.position = new Vector3(m_CameraTarget.position.x, m_CameraTarget.position.y, -m_DefaultCamDist);
    5. m_Camera.transform.position = m_CineVirtualCamera.transform.position;
    6.  
    But it still damp the position....
    Did I miss something ?
     
    Last edited: Sep 30, 2020
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,526
  5. Arkasis

    Arkasis

    Joined:
    Nov 22, 2010
    Posts:
    59
    Sorry, i forget to paste all code lines.
    I read a lot of your post and I was unable to find the good method...

    Code (CSharp):
    1. m_CineVirtualCamera.PreviousStateIsValid = false;
    2. m_CineVirtualCamera.OnTargetObjectWarped(m_CineVirtualCamera.transform, m_FollowedPlayer.transform.position - m_CameraTarget.position);  
    3. m_CineVirtualCamera.OnTargetObjectWarped(m_CameraTarget, m_FollowedPlayer.transform.position - m_CameraTarget.position);  
    4. m_CineVirtualCamera.OnTargetObjectWarped(m_Camera.transform, m_FollowedPlayer.transform.position - m_CameraTarget.position);
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,526
    You're calling it wrong. The delta position is the player position delta. The camera will worry about its own position. And you don't need to set PreviousStateIsValid = false.
    Code (CSharp):
    1. Transform target = m_FollowedPlayer;
    2. Vector3 warpDelta = FollowPlayerNewPosition - FollowPlayerOldPosition;
    3.  
    4. target.position += warpDelta; // warp the target
    5. m_CineVirtualCamera.OnTargetObjectWarped(target, warpDelta); // inform vcam that target was warped
     
  7. Arkasis

    Arkasis

    Joined:
    Nov 22, 2010
    Posts:
    59
    I did a messy melting pot with all the solutions I found xD
    Thanks a lot, it is now working :)
     
    Gregoryl likes this.
  8. Arkasis

    Arkasis

    Joined:
    Nov 22, 2010
    Posts:
    59
    Sorry, it is me again...
    It is not working anymore and I don't know why.
    I just deactivate / activate GameObjects to initialize the scene and the camera now more teleport to the targeted position.
    Here is my code :
    Code (CSharp):
    1. Vector3 deltaPos = m_FollowedPlayer.transform.position - m_CameraTarget.position;
    2. m_CameraTarget.position += deltaPos;
    3. m_CineVirtualCamera.OnTargetObjectWarped(m_CameraTarget, deltaPos);
    The deltaPos value is okay, I debug.log it.
    m_CameraTarget is set too.
    Everything is fine but the camera just do not teleport :(
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,526
    The vcam will only warp if m_CameraTarget is in fact its current target. Is it? Or is m_FollowedPlayer the current target at the time of the call?
     
  10. Arkasis

    Arkasis

    Joined:
    Nov 22, 2010
    Posts:
    59
    Yes, m_CameraTarget is the element followed by the camera.
    I just log it and it is the current target when OnTargetObjectWarped is called.
    m_CameraTarget follow the player position and let me modifiy it position according to some gameplay rules.
    The virtual camera follow this target and not directly the player.
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,526
    Your code look fine. One thing I can think of that would make this not work is if m_CineVirtualCamera is not in fact the active virtual camera at the time of the warp. Apart from that, I would conclude that you are doing something that interferes with this elsewhere in your scripts.