Skip to content

Instantly share code, notes, and snippets.

@nevernotsean
Created February 7, 2023 00:40
Show Gist options
  • Save nevernotsean/2483928b528a568248bfacc10ac5f409 to your computer and use it in GitHub Desktop.
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…
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