Game Dev Stories

Using and Accessing Unity 6 Cinemachine Follow Offset

by theoryoflinus

The OLD method of accessing the cinemachine body’s follow offset was to

[SerializeField] private CinemachineVirtualCamera cinemachineVirtualCamera;
private CinemachineTransposer cinemachineTransposer;
  
cinemachineTransposer = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineTransposer>();
targetFollowOffset = cinemachineTransposer.m_FollowOffset; 

which seemed somewhat confusing to find.

For those who are using Unity 6 and Cinemachine 3.1,

1. “Follow” and “Hard Look At” Components

For old Cinemachine’s “Body” and “Aim”, I used “Follow” and “Hard Look At” Components to replace them.

CinemachineCamera -> Procedural Components -> Position Control -> Follow (Component)
CinemachineCamera -> Procedural Components -> Rotation Control -> Hard Look At

2. For accessing “Follow Offset”, Use “CinemachineFollow” component

  • If you want to have a cinemachineCamara field,
[SerializeField] private CinemachineCamera cinemachineCamera;
cinemachineFollow = cinemachineCamera.GetComponent<CinemachineFollow>();
followOffset = cinemachineFollow.FollowOffset; 
  • or if you just need a cinemachineFollow field,
[SerializeField] private CinemachineFollow cinemachineFollow; 
followOffset = cinemachineFollow.FollowOffset;

3. Sample code

using Unity.Cinemachine;

private const float MIN_FOLLOW_Y_OFFSET = 2f;
private const float MAX_FOLLOW_Y_OFFSET = 12f;
[SerializeField] private CinemachineFollow cinemachineFollow; // Just used the Cinemachine Camara object
private Vector3 targetFollowOffset;

private void Start() {
    targetFollowOffset = cinemachineFollow.FollowOffset;
}

private void Update() {
    var zoomAmount = 1f;
    var zoomSpeed = 5f; 
    if (Input.mouseScrollDelta.y > 0) {
        targetFollowOffset.y -= zoomAmount;
    }

    if (Input.mouseScrollDelta.y < 0) {
        targetFollowOffset.y += zoomAmount;
    }
    targetFollowOffset.y = Mathf.Clamp(targetFollowOffset.y, MIN_FOLLOW_Y_OFFSET, MAX_FOLLOW_Y_OFFSET);
    cinemachineFollow.FollowOffset = Vector3.Lerp(cinemachineFollow.FollowOffset, targetFollowOffset, Time.deltaTime * zoomSpeed);
}
  • The original code was from @UnityCodeMonkey’s tutorial, and I modified it to fit the new Cinemachine.

4. Extra thoughts during development

  • Input.mouseScrollDelta is an event-based input and resets every frame, which can cause missed scroll events when scrolling quickly. Using Input.GetAxis(“Mouse ScrollWheel”) could provide more reliable detection.
  • Cache the component reference when possible to improve performance and readability.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy