Skip to content

Instantly share code, notes, and snippets.

@TheJustLink
Last active January 21, 2022 09:16
Show Gist options
  • Save TheJustLink/46d97d16cbbc7e475a0db1b8dbf3ec4b to your computer and use it in GitHub Desktop.
Save TheJustLink/46d97d16cbbc7e475a0db1b8dbf3ec4b to your computer and use it in GitHub Desktop.
Lock Cinemachine Follow Extension for Unity csharp C#. Lock any axis for cinemachines follow. You can choose which axis you want to lock and what value to lock for your cinemachine.
using UnityEngine;
using Cinemachine;
using static Cinemachine.CinemachineCore;
[SaveDuringPlay]
[ExecuteInEditMode]
public class LockCinemachineFollow : CinemachineExtension
{
[SerializeField] private Vector3 _lockPosition;
[SerializeField] private bool _lockX;
[SerializeField] private bool _lockY;
[SerializeField] private bool _lockZ;
protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, Stage stage, ref CameraState state, float deltaTime)
{
if (CanLockPosition(stage))
LockPosition(ref state);
}
private bool CanLockPosition(Stage stage)
{
return enabled && stage == Stage.Body;
}
private void LockPosition(ref CameraState state)
{
state.RawPosition = GetLockedPosition(state.RawPosition);
}
private Vector3 GetLockedPosition(Vector3 position)
{
return new Vector3(
_lockX ? _lockPosition.x : position.x,
_lockY ? _lockPosition.y : position.y,
_lockZ ? _lockPosition.z : position.z
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment