I’m trying to control the camera offset using the right thumb stick.
Currently have a 3d game where the player is controlling the movement with the left thumb stick. The cinemachine camera is setup right now the Position Composer where there screen position is set 0.2 on the Y giving the character position in the lower third of the screen. I have target tracking setup for Lookahead too and this is working great.
Now, I’ve tried a few things, one was creating a camera follow gameObject that mapped to the player position, then I could control that position relative to the player and the camera moved around quite well, but trying to then get Lookahead working was terrible since as I moved the camera follow object it would see that as the movement input. So it looks like I have to update the camera offset, however in doing so, the Y axis of the camera is relative to the direction the camera is pointing in. I can calculate this based off the player plane but is the the right approach?
So this seems to be a much better approach than what I was previously doing. Leaving it here incase it will help anyone in the future. I effectively added script to listen to the OnLook control and then had this update the Cinemachine camera cameraOffsetExtension offset.
// For analog input, calculate the desired offset directly
Vector3 inputVector = new Vector3(_lookInput.x, 0f, _lookInput.y);
// Rotate the input vector by the camera's yaw rotation
float cameraYaw = cinemachineCamera.transform.eulerAngles.y;
Quaternion yawRotation = Quaternion.Euler(0f, cameraYaw, 0f);
Vector3 movementWorld = yawRotation * inputVector * maxOffsetDistance;
// Transform to camera's local space
_desiredOffset = cinemachineCamera.transform.InverseTransformDirection(movementWorld);
// Clamp the desired offset
_desiredOffset = Vector3.ClampMagnitude(_desiredOffset, maxOffsetDistance);
// Smoothly move currentOffset towards desiredOffset
_currentOffset = Vector3.SmoothDamp(_currentOffset, _desiredOffset, ref _offsetVelocity, smoothTime);
_cameraOffsetExtension.Offset = _currentOffset;
Additionally to have it snap back
private void OnLook(Vector2 lookValue) {
if (lookValue.magnitude < deadZone) {
_lookInput = Vector2.zero;
} else {
_lookInput = lookValue;
}
}
1 Like