Skip to content

Instantly share code, notes, and snippets.

@aviralgoel
Created October 29, 2022 02:14
Show Gist options
  • Save aviralgoel/d6ee3b01aac6e7688672947f894bdcc5 to your computer and use it in GitHub Desktop.
Save aviralgoel/d6ee3b01aac6e7688672947f894bdcc5 to your computer and use it in GitHub Desktop.
Change Camera Rotation by User Input when Camera is Framing Transposer in Unity Cinemachine
// Problem Statment: Allow the player to modify camera orientation during runtime using key press
// Since Framing Transposer Virtual Camera Target Mode by default does not allow altering camera rotation
// This script works as solution
// 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 CameraRotation : 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;
// my game input system, you don't need it
public RPGCharacterInputSystemController c;
protected override void PostPipelineStageCallback(
CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
{
if (stage == CinemachineCore.Stage.Aim)
{
var pos = state.RawOrientation;
if (c.inputCameraPan) // use Input.GetKey here or similiar funtion to detect user input
{
// final look at point
Quaternion lookAt = Quaternion.Euler(m_XDefaultRotation, PanCamRotationInY, m_ZDefaultRotation);
// slowly rotate between current rotation to lookAt
state.RawOrientation = Quaternion.Lerp(transform.rotation, lookAt, cameraPanSpeed * Time.deltaTime);
}
else
{
state.RawOrientation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(m_XDefaultRotation, m_YDefaultRotation, m_ZDefaultRotation), cameraResetSpeed * Time.deltaTime);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment