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.
  2. Dismiss Notice

Floating Origin with Cinemachine

Discussion in 'Scripting' started by mvinc006, Mar 31, 2019.

  1. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    Hi All,

    I'm having trouble with my Cinemachine camera that pops (non smooth transition) back to target when I reset my player to origin.

    Did some digging and found that others have had this problem and was given a solution (https://forum.unity.com/threads/rep...mera-runtime-how-to-avoid-the-popping.514293/).

    I've tried to implement the script in my own project however I am still getting the popping effect, I'll post what I've tried and the solution below.

    Solution:
    Code (CSharp):
    1.             // Inform any vcams that might be tracking this target
    2.             int numVcams = CinemachineCore.Instance.VirtualCameraCount;
    3.             for (int i = 0; i < numVcams; ++i)
    4.                 CinemachineCore.Instance.GetVirtualCamera(i).OnTargetObjectWarped(
    5.                     transform, posDelta);

    My Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6.  
    7. public class FloatingOrigin : MonoBehaviour
    8. {
    9.     public Transform Universe;
    10.     public Transform Player;
    11.  
    12.     [Range(10, 5000)]
    13.     public float unitsPerReset;
    14.  
    15.     private void FixedUpdate()
    16.     {            
    17.         Vector3 offset = Player.position - Vector3.zero;    
    18.  
    19.         float sqrLen = offset.sqrMagnitude;
    20.  
    21.             if(sqrLen >= unitsPerReset * unitsPerReset)
    22.             {
    23.                 updateCam(offset);
    24.                 Universe.transform.position -= (Player.transform.position);        
    25.                 Player.transform.position = Vector3.zero;                            
    26.                 Debug.Log("Reset Ticked", gameObject);
    27.             }                
    28.     }
    29.  
    30.     private void updateCam(Vector3 posDelta)
    31.     {
    32.         int numVcams = CinemachineCore.Instance.VirtualCameraCount;
    33.         for (int i = 0; i < numVcams; ++i)
    34.         {
    35.             CinemachineCore.Instance.GetVirtualCamera(i).OnTargetObjectWarped(Player, posDelta);
    36.         }        
    37.     }
    38. }
    Looking at the official documentation, it does appear I am using it correctly (https://docs.unity3d.com/Packages/c...tBase_OnTargetObjectWarped_Transform_Vector3_)
     
    Last edited: Mar 31, 2019
  2. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    Ahh managed to solve it myself with a couple tweaks. The main issue was the posDelta needed to be inverted to a negative value. Although this was just a poorly implemented FloatingOrigin Solution, it was the backbones of what I needed to get moving forward.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6.  
    7. public class FloatingOrigin : MonoBehaviour
    8. {
    9.     public Transform Universe;
    10.     public Transform Player;
    11.     public bool Debugging;
    12.     private Rigidbody rb;
    13.  
    14.     [Range(10, 5000)]
    15.     public float unitsPerReset;
    16.  
    17.     private void Start()
    18.     {
    19.         rb = Player.GetComponent<Rigidbody>();
    20.     }
    21.  
    22.     private void FixedUpdate()
    23.     {
    24.         Vector3 offset = Player.position;
    25.         float sqrLen = offset.sqrMagnitude;
    26.  
    27.             if(sqrLen >= unitsPerReset * unitsPerReset)
    28.             {              
    29.                 Universe.transform.position -= (Player.transform.position);          
    30.                 rb.MovePosition(Vector3.zero);
    31.                 updateCam(offset);
    32.  
    33.                 if (Debugging) { Debug.Log("Floating Origin Ticked", gameObject); }
    34.             }                
    35.     }
    36.  
    37.     private void updateCam(Vector3 posDelta)
    38.     {
    39.         int numVcams = CinemachineCore.Instance.VirtualCameraCount;
    40.         for (int i = 0; i < numVcams; ++i)
    41.         {        
    42.             CinemachineCore.Instance.GetVirtualCamera(i).OnTargetObjectWarped(Player, -posDelta);
    43.         }          
    44.     }
    45. }