Last active
October 29, 2022 02:12
-
-
Save aviralgoel/ba882a0843360acf5653a216b429b9a5 to your computer and use it in GitHub Desktop.
Unity Cinemachine Setting Virtual Camera Orientation when using Framing Transposer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem Statement: When using Framing Transposer to follow a target via virtual camera | |
// framing transposer does not allow altering rotation of the camera. | |
// this script let us set a initial rotation of the camera and then allow us to manipulate vcam orientation during runtime | |
// no Aim target gameObject is necessary. | |
// After dropping the script into your project, add it to virtual camera through Virtual Camera Extention field | |
// in the Virtual Camera Gameobject | |
using UnityEngine; | |
using Cinemachine; | |
//using TryAgainFSM; | |
/// <summary> | |
/// An add-on module for Cinemachine Virtual Camera that locks the camera's X (or Y/ or Z) rotation | |
/// </summary> | |
[SaveDuringPlay] | |
[AddComponentMenu("")] // Hide in menu | |
public class LockCameraRotation : CinemachineExtension | |
{ | |
[Header("Defaut Camera Rotation")] | |
[Tooltip("Lock the camera's rotation to these values as default orientation")] | |
public float m_XDefaultRotation = 10f; | |
public float m_YDefaultRotation = 0f; | |
public float m_ZDefaultRotation = 0f; | |
[Header("Camera Panning Settings")] | |
public float PanCamRotationInY = 20f; // How far can this camera pan? | |
public float cameraPanSpeed = 5f; | |
public float cameraResetSpeed = 20f; | |
//public RPGCharacterInputSystemController c; | |
protected override void PostPipelineStageCallback( | |
CinemachineVirtualCameraBase vcam, | |
CinemachineCore.Stage stage, ref CameraState state, float deltaTime) | |
{ | |
if (stage == CinemachineCore.Stage.Aim) // runs every frame | |
{ | |
var pos = state.RawOrientation; | |
// you can modify camera rotation by changing pos values | |
pos = Quaternion.Euler(m_XDefaultRotation, m_YDefaultRotation, m_ZDefaultRotation); | |
state.RawOrientation = pos; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment