I was searching for a way to lock the camera’s X position when I reached a certain point in my scene, and I found this extension script that works quite well. Here’s my modified version -
using UnityEngine;
using Cinemachine;
/// <summary>
/// An add-on module for Cinemachine Virtual Camera that locks the camera's X co-ordinate
/// </summary>
[ExecuteInEditMode]
[SaveDuringPlay]
[AddComponentMenu("")] // Hide in menu
public class LockCameraX : CinemachineExtension
{
[Tooltip("Lock the camera's X position to this value")]
public float m_XPosition = 0;
protected override void OnEnable()
{
base.OnEnable();
m_XPosition = transform.position.x;
}
protected override void PostPipelineStageCallback(
CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
{
if (stage == CinemachineCore.Stage.Body)
{
var pos = state.RawPosition;
pos.x = m_XPosition;
state.RawPosition = pos;
}
}
}
This script is attached to my vcam. Whenever my player runs into a trigger point, I enable this script, and the camera gets locked to the X position it was at when enabled. However, the problem comes when I disable this script after exiting that trigger. The trigger is set up so that when entering, this script is enabled, and then when exiting, the script is disabled. Again, this works really well when the script is enabled, and locks to the X position nicely.
The image shows my Body property settings. My camera moves ahead of the player when switching directions.
Is there anything I can change to stop the jerking motion when the script is disabled again? I’m assuming it’s basically snapping back to the player after disabling the script.
I imagine you have a follow camera of some sort, let’s call it FollowCam. When your player enters the trigger, you enable LockCameraX script on this FollowCam. When the player leaves the trigger, you disable LockCameraX on this FollowCam. But this causes a bad transition from lockX to unlockX state.
Instead of having just 1 vcam, let’s have 2: FollowCam1 and FollowCam2. These vcams are identical, except that FollowCam2 has LockX script on it. Disable FollowCam2 and disable the LockX script on it.
When the player enters the trigger, enable FollowCam2 and enable the LockCameraX script on it. When the player leaves the trigger zone, disable FollowCam2 and disable LockCameraX script on it.
Now the switch between FollowCam2 to FollowCam1 is going to be defined by your Blend settings. By default, this is going to be an Ease In - 2 seconds.
Now let’s customize your blends. Go to your Main Camera, and find CinemachineBrain. Next to the Custom Blend field, click on Create Asset.
Then set the blend From FollowCam1 To FollowCam2 to cut. These 2 are identical when they switch, so you can cut without worry.
Then set the blend From FollowCam2 To FollowCam1 to, for example, Ease In - 1 second.
This is very good advice, but I would change one thing: leave the Lock X script enabled on FollowCam2. If you disable it when transitioning, you will get a position pop. With 2 vcams, there is no reason to disable the Lock X script.