case1
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
|
||||
[AddComponentMenu("")] // Don't display in add component menu
|
||||
public class ActivateCamOnPlay : MonoBehaviour
|
||||
{
|
||||
public CinemachineVirtualCameraBase vcam;
|
||||
|
||||
// Use this for initialization
|
||||
void Start ()
|
||||
{
|
||||
if (vcam)
|
||||
{
|
||||
vcam.MoveToTopOfPrioritySubqueue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2cfb0b06d5a4af5429ea7f2b67fd8579
|
||||
timeCreated: 1509461617
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,47 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
|
||||
[AddComponentMenu("")] // Don't display in add component menu
|
||||
public class ActivateCameraWithDistance : MonoBehaviour
|
||||
{
|
||||
public GameObject objectToCheck;
|
||||
public float distanceToObject = 15f;
|
||||
public CinemachineVirtualCameraBase initialActiveCam;
|
||||
public CinemachineVirtualCameraBase switchCameraTo;
|
||||
|
||||
CinemachineBrain brain;
|
||||
|
||||
void Start()
|
||||
{
|
||||
brain = Camera.main.GetComponent<CinemachineBrain>();
|
||||
SwitchCam(initialActiveCam);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (objectToCheck && switchCameraTo)
|
||||
{
|
||||
if (Vector3.Distance(transform.position, objectToCheck.transform.position) < distanceToObject)
|
||||
{
|
||||
SwitchCam(switchCameraTo);
|
||||
}
|
||||
else
|
||||
{
|
||||
SwitchCam(initialActiveCam);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SwitchCam(CinemachineVirtualCameraBase vcam)
|
||||
{
|
||||
if (brain == null || vcam == null)
|
||||
return;
|
||||
if (brain.ActiveVirtualCamera != (ICinemachineCamera)vcam)
|
||||
vcam.MoveToTopOfPrioritySubqueue();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 36562141f3e01c7499f91e66f806814c
|
||||
timeCreated: 1508854172
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
|
||||
[AddComponentMenu("")] // Don't display in add component menu
|
||||
public class ExampleHelpWindow : MonoBehaviour
|
||||
{
|
||||
public string m_Title;
|
||||
[TextArea(minLines: 10, maxLines: 50)]
|
||||
public string m_Description;
|
||||
|
||||
private bool mShowingHelpWindow = true;
|
||||
private const float kPadding = 40f;
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (mShowingHelpWindow)
|
||||
{
|
||||
Vector2 size = GUI.skin.label.CalcSize(new GUIContent(m_Description));
|
||||
Vector2 halfSize = size * 0.5f;
|
||||
|
||||
float maxWidth = Mathf.Min(Screen.width - kPadding, size.x);
|
||||
float left = Screen.width * 0.5f - maxWidth * 0.5f;
|
||||
float top = Screen.height * 0.4f - halfSize.y;
|
||||
|
||||
Rect windowRect = new Rect(left, top, maxWidth, size.y);
|
||||
GUILayout.Window(400, windowRect, (id) => DrawWindow(id, maxWidth), m_Title);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawWindow(int id, float maxWidth)
|
||||
{
|
||||
GUILayout.BeginVertical(GUI.skin.box);
|
||||
GUILayout.Label(m_Description);
|
||||
GUILayout.EndVertical();
|
||||
if (GUILayout.Button("Got it!"))
|
||||
{
|
||||
mShowingHelpWindow = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 803b00b1c55094e499a99684ddbd9b57
|
||||
timeCreated: 1464971222
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,23 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
public static class InputSystemHelper
|
||||
{
|
||||
static float s_LastMessageTime = -10;
|
||||
// Logs warning every 5 seconds
|
||||
public static void EnableBackendsWarningMessage()
|
||||
{
|
||||
if (Time.realtimeSinceStartup - s_LastMessageTime > 5)
|
||||
{
|
||||
Debug.Log(
|
||||
"Old input backends are disabled. Cinemachine examples use Unity’s classic input system. " +
|
||||
"To enable classic input: Edit > Project Settings > Player, " +
|
||||
"set Active Input Handling to Both!\n" +
|
||||
"Note: This is probably because the Input System Package has been added to the project. To use " +
|
||||
"Cinemachine Virtual Cameras with the Input System Package, add CinemachineInputProvider component to them.");
|
||||
s_LastMessageTime = Time.realtimeSinceStartup;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5204e51f84074d79a29384cce2de3747
|
||||
timeCreated: 1636397410
|
||||
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using Cinemachine.Editor;
|
||||
#endif
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
public class InvokeEventFromInspector : MonoBehaviour
|
||||
{
|
||||
public UnityEvent Event = new UnityEvent();
|
||||
public void Invoke() { Event.Invoke(); }
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[CustomEditor(typeof(InvokeEventFromInspector))]
|
||||
public class GenerateEventEditor : BaseEditor<InvokeEventFromInspector>
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
BeginInspector();
|
||||
Rect rect = EditorGUILayout.GetControlRect(true);
|
||||
if (GUI.Button(rect, "Invoke", "Button"))
|
||||
Target.Invoke();
|
||||
DrawRemainingPropertiesInInspector();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38ed7ef6fdf27884498af1842cbb6799
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
|
||||
[AddComponentMenu("")] // Don't display in add component menu
|
||||
public class MixingCameraBlend : MonoBehaviour
|
||||
{
|
||||
public enum AxisEnum {X,Z,XZ};
|
||||
|
||||
public Transform followTarget;
|
||||
public float initialBottomWeight = 20f;
|
||||
public AxisEnum axisToTrack;
|
||||
|
||||
private CinemachineMixingCamera vcam;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (followTarget)
|
||||
{
|
||||
vcam = GetComponent<CinemachineMixingCamera>();
|
||||
vcam.m_Weight0 = initialBottomWeight;
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (followTarget)
|
||||
{
|
||||
switch (axisToTrack)
|
||||
{
|
||||
case (AxisEnum.X):
|
||||
vcam.m_Weight1 = Mathf.Abs(followTarget.transform.position.x);
|
||||
break;
|
||||
case (AxisEnum.Z):
|
||||
vcam.m_Weight1 = Mathf.Abs(followTarget.transform.position.z);
|
||||
break;
|
||||
case (AxisEnum.XZ):
|
||||
vcam.m_Weight1 =
|
||||
Mathf.Abs(Mathf.Abs(followTarget.transform.position.x) +
|
||||
Mathf.Abs(followTarget.transform.position.z));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23645a9e62f352c4aa1ddb8e55df7c3b
|
||||
timeCreated: 1505843643
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using Cinemachine.Utility;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
public class PlayerMove : MonoBehaviour
|
||||
{
|
||||
public float Speed;
|
||||
public float VelocityDamping;
|
||||
public float JumpTime;
|
||||
|
||||
public enum ForwardMode
|
||||
{
|
||||
Camera,
|
||||
Player,
|
||||
World
|
||||
};
|
||||
|
||||
public ForwardMode InputForward;
|
||||
|
||||
public bool RotatePlayer = true;
|
||||
|
||||
public Action SpaceAction;
|
||||
public Action EnterAction;
|
||||
|
||||
Vector3 m_currentVleocity;
|
||||
float m_currentJumpSpeed;
|
||||
float m_restY;
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
Speed = 5;
|
||||
InputForward = ForwardMode.Camera;
|
||||
RotatePlayer = true;
|
||||
VelocityDamping = 0.5f;
|
||||
m_currentVleocity = Vector3.zero;
|
||||
JumpTime = 1;
|
||||
m_currentJumpSpeed = 0;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
m_currentJumpSpeed = 0;
|
||||
m_restY = transform.position.y;
|
||||
SpaceAction -= Jump;
|
||||
SpaceAction += Jump;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
Vector3 fwd;
|
||||
switch (InputForward)
|
||||
{
|
||||
case ForwardMode.Camera:
|
||||
fwd = Camera.main.transform.forward;
|
||||
break;
|
||||
case ForwardMode.Player:
|
||||
fwd = transform.forward;
|
||||
break;
|
||||
case ForwardMode.World:
|
||||
default:
|
||||
fwd = Vector3.forward;
|
||||
break;
|
||||
}
|
||||
|
||||
fwd.y = 0;
|
||||
fwd = fwd.normalized;
|
||||
if (fwd.sqrMagnitude < 0.01f)
|
||||
return;
|
||||
|
||||
Quaternion inputFrame = Quaternion.LookRotation(fwd, Vector3.up);
|
||||
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
|
||||
input = inputFrame * input;
|
||||
|
||||
var dt = Time.deltaTime;
|
||||
var desiredVelocity = input * Speed;
|
||||
var deltaVel = desiredVelocity - m_currentVleocity;
|
||||
m_currentVleocity += Damper.Damp(deltaVel, VelocityDamping, dt);
|
||||
|
||||
transform.position += m_currentVleocity * dt;
|
||||
if (RotatePlayer && m_currentVleocity.sqrMagnitude > 0.01f)
|
||||
{
|
||||
var qA = transform.rotation;
|
||||
var qB = Quaternion.LookRotation(
|
||||
(InputForward == ForwardMode.Player && Vector3.Dot(fwd, m_currentVleocity) < 0)
|
||||
? -m_currentVleocity
|
||||
: m_currentVleocity);
|
||||
transform.rotation = Quaternion.Slerp(qA, qB, Damper.Damp(1, VelocityDamping, dt));
|
||||
}
|
||||
|
||||
// Process jump
|
||||
if (m_currentJumpSpeed != 0)
|
||||
m_currentJumpSpeed -= 10 * dt;
|
||||
var p = transform.position;
|
||||
p.y += m_currentJumpSpeed * dt;
|
||||
if (p.y < m_restY)
|
||||
{
|
||||
p.y = m_restY;
|
||||
m_currentJumpSpeed = 0;
|
||||
}
|
||||
|
||||
transform.position = p;
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Space) && SpaceAction != null)
|
||||
SpaceAction();
|
||||
if (Input.GetKeyDown(KeyCode.Return) && EnterAction != null)
|
||||
EnterAction();
|
||||
#else
|
||||
InputSystemHelper.EnableBackendsWarningMessage();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Jump()
|
||||
{
|
||||
m_currentJumpSpeed += 10 * JumpTime * 0.5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa14cc4c274b78b4c820064214bdbb60
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
using Cinemachine.Utility;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
public class PlayerMoveOnSphere : MonoBehaviour
|
||||
{
|
||||
public SphereCollider Sphere;
|
||||
|
||||
public float speed = 5;
|
||||
public bool rotatePlayer = true;
|
||||
public float rotationDamping = 0.5f;
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
|
||||
if (input.magnitude > 0)
|
||||
{
|
||||
input = Camera.main.transform.rotation * input;
|
||||
if (input.magnitude > 0.001f)
|
||||
{
|
||||
transform.position += input * (speed * Time.deltaTime);
|
||||
if (rotatePlayer)
|
||||
{
|
||||
float t = Cinemachine.Utility.Damper.Damp(1, rotationDamping, Time.deltaTime);
|
||||
Quaternion newRotation = Quaternion.LookRotation(input.normalized, transform.up);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stick to sphere surface
|
||||
if (Sphere != null)
|
||||
{
|
||||
var up = transform.position - Sphere.transform.position;
|
||||
up = up.normalized;
|
||||
var fwd = transform.forward.ProjectOntoPlane(up);
|
||||
transform.position = Sphere.transform.position + up * (Sphere.radius + transform.localScale.y / 2);
|
||||
transform.rotation = Quaternion.LookRotation(fwd, up);
|
||||
}
|
||||
#else
|
||||
InputSystemHelper.EnableBackendsWarningMessage();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 85b757e40164bd3408008c7df6b949d5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
public class PlayerMovePhysics : MonoBehaviour
|
||||
{
|
||||
public float speed = 5;
|
||||
public bool worldDirection = true;
|
||||
public bool rotatePlayer = true;
|
||||
|
||||
public Action spaceAction;
|
||||
public Action enterAction;
|
||||
|
||||
Rigidbody rb;
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
transform.position += new Vector3(10, 0, 0);
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
|
||||
|
||||
//input = Vector3.forward;
|
||||
if (input.magnitude > 0)
|
||||
{
|
||||
Vector3 fwd = worldDirection
|
||||
? Vector3.forward
|
||||
: transform.position - Camera.main.transform.position;
|
||||
fwd.y = 0;
|
||||
fwd = fwd.normalized;
|
||||
if (fwd.magnitude > 0.001f)
|
||||
{
|
||||
Quaternion inputFrame = Quaternion.LookRotation(fwd, Vector3.up);
|
||||
input = inputFrame * input;
|
||||
if (input.magnitude > 0.001f)
|
||||
{
|
||||
rb.AddForce(speed * input);
|
||||
if (rotatePlayer)
|
||||
transform.rotation = Quaternion.LookRotation(input.normalized, Vector3.up);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Space) && spaceAction != null)
|
||||
spaceAction();
|
||||
if (Input.GetKeyDown(KeyCode.Return) && enterAction != null)
|
||||
enterAction();
|
||||
|
||||
#else
|
||||
InputSystemHelper.EnableBackendsWarningMessage();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73e68a7cb19ab0142b14f1d5b83e3bf0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,39 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
/// <summary>
|
||||
/// Moves transform between minY and maxY with the specified speed.
|
||||
/// </summary>
|
||||
public class SimpleElevator : MonoBehaviour
|
||||
{
|
||||
public float minY, maxY;
|
||||
public float speed;
|
||||
public bool on;
|
||||
|
||||
float m_Direction = 1;
|
||||
Rigidbody rb;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody>();
|
||||
rb.interpolation = RigidbodyInterpolation.Interpolate;
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (transform.position.y < minY)
|
||||
{
|
||||
m_Direction = 1f;
|
||||
}
|
||||
|
||||
if (transform.position.y > maxY)
|
||||
{
|
||||
m_Direction = -1f;
|
||||
}
|
||||
|
||||
var dir = new Vector3(0, m_Direction * speed * Time.fixedDeltaTime, 0);
|
||||
rb.MovePosition(rb.position += dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fde54f5cdf0dd46bbadfe8535c25f217
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
public class SpawnInCircle : MonoBehaviour
|
||||
{
|
||||
public GameObject Prefab;
|
||||
public float Radius = 1000;
|
||||
public float Amount = 10000;
|
||||
public bool DoIt;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (DoIt && Prefab != null)
|
||||
{
|
||||
var rootPos = transform.position;
|
||||
var rot = transform.rotation;
|
||||
for (int i = 0; i < Amount; ++i)
|
||||
{
|
||||
var a = Random.Range(0, 360);
|
||||
var pos = new Vector3(Mathf.Cos(a), 0, Mathf.Sin(a));
|
||||
pos = rootPos + pos * Mathf.Sqrt(Random.Range(0.0f, 1.0f)) * Radius;
|
||||
Instantiate(Prefab, pos, rot, transform.parent);
|
||||
}
|
||||
}
|
||||
|
||||
DoIt = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e25d65cce934ce44a5f01658c52ee8b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,68 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cinemachine.Examples
|
||||
{
|
||||
/// <summary>
|
||||
/// This is an add-on for Cinemachine virtual cameras containing the ThirdPersonFollow component.
|
||||
/// It modifies the camera distance as a function of vertical angle.
|
||||
/// </summary>
|
||||
[SaveDuringPlay]
|
||||
public class ThirdPersonFollowDistanceModifier : MonoBehaviour
|
||||
{
|
||||
[Tooltip("Camera angle that corresponds to the start of the distance graph")]
|
||||
public float MinAngle;
|
||||
|
||||
[Tooltip("Camera angle that corresponds to the end of the distance graph")]
|
||||
public float MaxAngle;
|
||||
|
||||
[Tooltip("Defines how the camera distance scales as a function of vertical camera angle. "
|
||||
+ "X axis of graph go from 0 to 1, Y axis is the multiplier that will be "
|
||||
+ "applied to the base distance.")]
|
||||
public AnimationCurve DistanceScale;
|
||||
|
||||
Cinemachine3rdPersonFollow TpsFollow;
|
||||
Transform FollowTarget;
|
||||
float BaseDistance;
|
||||
|
||||
void Reset()
|
||||
{
|
||||
MinAngle = -90;
|
||||
MaxAngle = 90;
|
||||
DistanceScale = AnimationCurve.EaseInOut(0, 0.5f, 1, 2);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
var vcam = GetComponentInChildren<CinemachineVirtualCamera>();
|
||||
if (vcam != null)
|
||||
{
|
||||
TpsFollow = vcam.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
|
||||
FollowTarget = vcam.Follow;
|
||||
}
|
||||
|
||||
// Store the base camera distance, for consistent scaling
|
||||
if (TpsFollow != null)
|
||||
BaseDistance = TpsFollow.CameraDistance;
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
// Restore the TPS base camera distance
|
||||
if (TpsFollow != null)
|
||||
TpsFollow.CameraDistance = BaseDistance;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Scale the TPS camera distance
|
||||
if (TpsFollow != null && FollowTarget != null)
|
||||
{
|
||||
var xRot = FollowTarget.rotation.eulerAngles.x;
|
||||
if (xRot > 180)
|
||||
xRot -= 360;
|
||||
var t = (xRot - MinAngle) / (MaxAngle - MinAngle);
|
||||
TpsFollow.CameraDistance = BaseDistance * DistanceScale.Evaluate(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 546027855273aed40809fd72c01c960a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user