Cinemachine follow player lock axis

Hi everyone. I am making a game in which you have a player that needs to go up a big tower by jumping.

I use a cinemachine virtual camera to follow the player around when he jumps and falls. I now ran into the problem that the camera also follows the player when moving in the x-axis when instead i would like to have the camera be locked to x = 0. I found a script that is supposed to work from someone that had a similar problem.

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 cameralock : CinemachineExtension
{
    [Tooltip("Lock the camera's X position to this value")]
    public float m_XPosition = 0;

    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 doesn’t do anything for me though. Anyone know why that might be the case?

Thanks in advance :slight_smile:

2 Likes

Hi,
Could you send an image of your virtual camera, and all its components?
It should work except if you are using Framing Transposer, I think.

If you are using Framing Transposer, then instead of Body, use Finalize.

if (stage == CinemachineCore.Stage.Body)
 if (stage == CinemachineCore.Stage.Finalize)

This way even with Framing Transposer position X will be locked, but you will lose noise that would effect X, if you use noise.

4 Likes

Thank you so much!

I indeed use Framing Transpoder and it works with finalize.
Youre amazing much love :heart:

2 Likes