Created
February 7, 2023 00:40
-
-
Save nevernotsean/2483928b528a568248bfacc10ac5f409 to your computer and use it in GitHub Desktop.
For use with the Dreamteck Forever asset. With the FloatingOrigin class, when the camera reaches a certain distance from the origin, it will move the entire LevelGenerator and children back to the world origin. This is to keep floating point precision in tact. Cinemachine doesnt like this, if there is any damping to your Virtual Camera body tran…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using Cinemachine; | |
using UnityEngine; | |
using Dreamteck.Forever; | |
[RequireComponent(typeof(CinemachineBrain))] | |
public class FloatingOriginCinemachineCamera : MonoBehaviour | |
{ | |
CinemachineBrain brain; | |
CinemachineTransposer transposer; | |
private void Awake() | |
{ | |
brain = GetComponent<CinemachineBrain>(); | |
} | |
private void OnEnable() | |
{ | |
FloatingOrigin.onOriginOffset += OnOriginOffset; | |
} | |
private void OnDisable() | |
{ | |
FloatingOrigin.onOriginOffset -= OnOriginOffset; | |
} | |
private void OnDestroy() | |
{ | |
OnDisable(); | |
} | |
void OnOriginOffset(Vector3 delta) | |
{ | |
if (brain?.ActiveVirtualCamera != null) | |
if (brain.ActiveVirtualCamera is CinemachineVirtualCamera virtualCamera) | |
{ | |
transposer = virtualCamera.GetCinemachineComponent<CinemachineTransposer>(); | |
if (transposer == null) return; | |
var nextPosition = virtualCamera.transform.position - delta; | |
transposer.ForceCameraPosition(nextPosition, transposer.transform.rotation); | |
StartCoroutine(DelayedUpdate(delta)); | |
} | |
} | |
IEnumerator DelayedUpdate(Vector3 delta) | |
{ | |
var originalUpdate = brain.m_UpdateMethod; | |
brain.m_UpdateMethod = CinemachineBrain.UpdateMethod.ManualUpdate; | |
brain.ManualUpdate(); | |
yield return new WaitForEndOfFrame(); | |
brain.m_UpdateMethod = originalUpdate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment