I have a cinemachine virtual camera that attach to my player and this is the settings
The output of this setting is it follows the model without rotating the camera and thats what I need. Now I added this script
using Cinemachine;
using UnityEngine;
public class OrbitalTest : CinemachineExtension {
private const float RotateSpeed = 2f;
private Vector2 _currentRotate;
private Vector3 _distanceVector;
private float _currentDistance = 3f;
private Transform _lookAt;
private CinemachineVirtualCamera _virtualCamera;
private void Update() {
HandleInput();
// Rotate the camera
var centerMatrix = Matrix4x4.Rotate(Quaternion.Euler(_currentRotate.y, _currentRotate.x, 0f));
_distanceVector.z = -_currentDistance;
var pos = centerMatrix.MultiplyPoint3x4(_distanceVector);
// Update camera position and orientation
_virtualCamera.transform.position = _lookAt.position + pos;
_virtualCamera.transform.LookAt(_lookAt.position);
}
private void HandleInput() {
if (Input.GetKey(KeyCode.LeftArrow)) Rotate(new Vector2(1f, 0f));
if (Input.GetKey(KeyCode.RightArrow)) Rotate(new Vector2(-1f, 0f));
if (Input.GetKey(KeyCode.UpArrow)) Rotate(new Vector2(0f, 1f));
if (Input.GetKey(KeyCode.DownArrow)) Rotate(new Vector2(0f, -1f));
if (Input.GetKey(KeyCode.W)) Scale(-0.1f);
if (Input.GetKey(KeyCode.S)) Scale(0.1f);
}
private void Rotate(Vector2 diffDelta) {
diffDelta *= RotateSpeed * Time.deltaTime * 30;
_currentRotate.x += diffDelta.y;
_currentRotate.y += diffDelta.x;
}
protected override void ConnectToVcam(bool connect) {
base.ConnectToVcam(connect);
if (!connect) return;
_lookAt = GameObject.Find("LookAt")?.transform;
if (_lookAt == null) {
_lookAt = new GameObject("LookAt").transform;
_lookAt.position = new Vector3(0f, 0.5f, 0f);
}
_virtualCamera = VirtualCamera as CinemachineVirtualCamera;
_virtualCamera.LookAt = _lookAt;
}
protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage, ref CameraState state, float deltaTime) {
var (pos, rotate) = GetTransform(_virtualCamera.LookAt.localToWorldMatrix);
state.RawPosition = pos;
state.RawOrientation = rotate;
// Look at the player's position
_virtualCamera.transform.LookAt(_lookAt.position);
}
private (Vector3 pos, Quaternion rotate) GetTransform(Matrix4x4 baseCenterMatrix) {
var centerMatrix = baseCenterMatrix * Matrix4x4.Rotate(Quaternion.AngleAxis(_currentRotate.y, Vector3.up) *
Quaternion.AngleAxis(_currentRotate.x, Vector3.right));
_distanceVector.z = -_currentDistance;
var pos = centerMatrix.MultiplyPoint3x4(_distanceVector);
return (pos, centerMatrix.rotation);
}
void Scale(float delta) {
_currentDistance += delta;
}
}
Now it overrides my existing settings in the inspector. How can I prevent it? Even just dont make the camera rotates when the player rotates