using Cinemachine; using MoreMountains.Tools; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace MoreMountains.Tools { /// /// A class used to store gyro properties per camera /// [Serializable] [AddComponentMenu("More Mountains/Tools/Cinemachine/MMGyroCam")] public class MMGyroCam { /// the bound cinemachine camera public CinemachineVirtualCamera Cam; /// the transform this camera should rotate around public Transform RotationCenter; /// the minimum rotation to apply to this camera (in degrees) public Vector2 MinRotation = new Vector2(-2f, -2f); /// the maximum rotation to apply to this camera (in degrees) public Vector2 MaxRotation = new Vector2(2f, 2f); /// the camera's initial angles [MMReadOnly] public Vector3 InitialAngles; /// the camera's initial position [MMReadOnly] public Vector3 InitialPosition; } /// /// Add this class to a camera rig (an empty object), bind some Cinemachine virtual cameras to it, and they'll move around the specified object as your gyro powered device moves /// public class MMGyroParallax : MMGyroscope { [Header("Cameras")] /// the list of cameras to move as the gyro moves public List Cams; protected Vector3 _newAngles; /// /// On start we initialize our rig /// protected override void Start() { base.Start(); Initialization(); } /// /// Grabs the cameras and stores their position /// public virtual void Initialization() { foreach (MMGyroCam cam in Cams) { cam.InitialAngles = cam.Cam.transform.localEulerAngles; cam.InitialPosition = cam.Cam.transform.position; } } /// /// On Update we move our cameras /// protected override void Update() { base.Update(); MoveCameras(); } /// /// Moves cameras around based on gyro input /// protected virtual void MoveCameras() { foreach (MMGyroCam cam in Cams) { float newX = MMMaths.Remap(LerpedCalibratedGyroscopeGravity.x, 0.5f, -0.5f, cam.MinRotation.x, cam.MaxRotation.x); float newY = MMMaths.Remap(LerpedCalibratedGyroscopeGravity.y, 0.5f, -0.5f, cam.MinRotation.y, cam.MaxRotation.y); _newAngles = cam.InitialAngles; _newAngles.x += newX; _newAngles.z += newY; cam.Cam.transform.position = cam.InitialPosition; cam.Cam.transform.localEulerAngles = cam.InitialAngles; cam.Cam.transform.RotateAround(cam.RotationCenter.transform.position, cam.RotationCenter.transform.up, _newAngles.x); cam.Cam.transform.RotateAround(cam.RotationCenter.transform.position, cam.RotationCenter.transform.right, _newAngles.z); cam.Cam.transform.LookAt(cam.RotationCenter.transform); } } } }