213 lines
5.9 KiB
C#
213 lines
5.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace UnityTemplateProjects
|
|
{
|
|
public class CameraFly : MonoBehaviour
|
|
{
|
|
private class CameraState
|
|
{
|
|
public float yaw;
|
|
|
|
public float pitch;
|
|
|
|
public float roll;
|
|
|
|
public float x;
|
|
|
|
public float y;
|
|
|
|
public float z;
|
|
|
|
public void SetFromTransform(Transform t)
|
|
{
|
|
pitch = t.eulerAngles.x;
|
|
yaw = t.eulerAngles.y;
|
|
roll = t.eulerAngles.z;
|
|
x = t.position.x;
|
|
y = t.position.y;
|
|
z = t.position.z;
|
|
}
|
|
|
|
public void Translate(Vector3 translation)
|
|
{
|
|
Vector3 vector = Quaternion.Euler(pitch, yaw, roll) * translation;
|
|
x += vector.x;
|
|
y += vector.y;
|
|
z += vector.z;
|
|
}
|
|
|
|
public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
|
|
{
|
|
yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
|
|
pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
|
|
roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
|
|
x = Mathf.Lerp(x, target.x, positionLerpPct);
|
|
y = Mathf.Lerp(y, target.y, positionLerpPct);
|
|
z = Mathf.Lerp(z, target.z, positionLerpPct);
|
|
}
|
|
|
|
public void UpdateTransform(Transform t)
|
|
{
|
|
t.eulerAngles = new Vector3(pitch, yaw, roll);
|
|
t.position = new Vector3(x, y, z);
|
|
}
|
|
}
|
|
|
|
private CameraState m_TargetCameraState = new CameraState();
|
|
|
|
private CameraState m_InterpolatingCameraState = new CameraState();
|
|
|
|
[Header("Movement Settings")]
|
|
[Tooltip("Exponential boost factor on translation, controllable by mouse wheel.")]
|
|
public float boost = 3.5f;
|
|
|
|
[Tooltip("Time it takes to interpolate camera position 99% of the way to the target.")]
|
|
[Range(0.001f, 1f)]
|
|
public float positionLerpTime = 0.2f;
|
|
|
|
[Header("Rotation Settings")]
|
|
[Tooltip("X = Change in mouse position.\nY = Multiplicative factor for camera rotation.")]
|
|
public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));
|
|
|
|
[Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target.")]
|
|
[Range(0.001f, 1f)]
|
|
public float rotationLerpTime = 0.01f;
|
|
|
|
[Tooltip("Whether or not to invert our Y axis for mouse input to rotation.")]
|
|
public bool invertY;
|
|
|
|
private float speed = 1f;
|
|
|
|
private void OnEnable()
|
|
{
|
|
m_TargetCameraState.SetFromTransform(base.transform);
|
|
m_InterpolatingCameraState.SetFromTransform(base.transform);
|
|
}
|
|
|
|
private Vector3 GetInputTranslationDirection()
|
|
{
|
|
Vector3 result = default(Vector3);
|
|
if (Gamepad.current != null)
|
|
{
|
|
if (Gamepad.current.rightStick.ReadValue().y > 0f)
|
|
{
|
|
result += Vector3.forward * Gamepad.current.rightStick.ReadValue().y;
|
|
}
|
|
if (Gamepad.current.rightStick.ReadValue().y < 0f)
|
|
{
|
|
result += Vector3.back * (0f - Gamepad.current.rightStick.ReadValue().y);
|
|
}
|
|
if (Gamepad.current.rightStick.ReadValue().x < 0f)
|
|
{
|
|
result += Vector3.left * (0f - Gamepad.current.rightStick.ReadValue().x);
|
|
}
|
|
if (Gamepad.current.rightStick.ReadValue().x > 0f)
|
|
{
|
|
result += Vector3.right * Gamepad.current.rightStick.ReadValue().x;
|
|
}
|
|
if (Gamepad.current.dpad.up.ReadValue() == 1f)
|
|
{
|
|
result += Vector3.up;
|
|
}
|
|
if (Gamepad.current.dpad.down.ReadValue() == 1f)
|
|
{
|
|
result += Vector3.down;
|
|
}
|
|
}
|
|
if (Input.GetKey(KeyCode.W))
|
|
{
|
|
result += Vector3.forward;
|
|
}
|
|
if (Input.GetKey(KeyCode.S))
|
|
{
|
|
result += Vector3.back;
|
|
}
|
|
if (Input.GetKey(KeyCode.A))
|
|
{
|
|
result += Vector3.left;
|
|
}
|
|
if (Input.GetKey(KeyCode.D))
|
|
{
|
|
result += Vector3.right;
|
|
}
|
|
if (Input.GetKey(KeyCode.Q))
|
|
{
|
|
result += Vector3.down;
|
|
}
|
|
if (Input.GetKey(KeyCode.E))
|
|
{
|
|
result += Vector3.up;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKey(KeyCode.LeftAlt))
|
|
{
|
|
return;
|
|
}
|
|
if (Input.mouseScrollDelta.y != 0f)
|
|
{
|
|
speed += Input.mouseScrollDelta.y * 0.02f;
|
|
speed = Mathf.Clamp01(speed);
|
|
}
|
|
if (Input.GetMouseButtonDown(1))
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
if (Input.GetMouseButton(1))
|
|
{
|
|
Vector2 vector = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (float)(invertY ? 1 : (-1)));
|
|
float num = mouseSensitivityCurve.Evaluate(vector.magnitude);
|
|
m_TargetCameraState.yaw += vector.x * num * speed;
|
|
m_TargetCameraState.pitch += vector.y * num * speed;
|
|
}
|
|
else if (Gamepad.current != null)
|
|
{
|
|
if (Gamepad.current.dpad.left.ReadValue() == 1f)
|
|
{
|
|
speed -= 0.1f;
|
|
speed = Mathf.Clamp(speed, 0f, 5f);
|
|
}
|
|
else if (Gamepad.current.dpad.right.ReadValue() == 1f)
|
|
{
|
|
speed += 0.1f;
|
|
speed = Mathf.Clamp(speed, 0f, 5f);
|
|
}
|
|
Vector2 vector2 = new Vector2(Gamepad.current.leftStick.ReadValue().x, Gamepad.current.leftStick.ReadValue().y * (float)(invertY ? 1 : (-1)));
|
|
float num2 = mouseSensitivityCurve.Evaluate(vector2.magnitude);
|
|
m_TargetCameraState.yaw += vector2.x * num2 * Mathf.Abs(Gamepad.current.leftStick.ReadValue().x);
|
|
m_TargetCameraState.pitch += vector2.y * num2 * Mathf.Abs(Gamepad.current.leftStick.ReadValue().y);
|
|
}
|
|
Vector3 translation = GetInputTranslationDirection() * Time.deltaTime * speed;
|
|
if (Input.GetKey(KeyCode.LeftShift))
|
|
{
|
|
translation *= 10f;
|
|
}
|
|
if (Gamepad.current != null)
|
|
{
|
|
if (Gamepad.current.leftTrigger.ReadValue() > 0.1f)
|
|
{
|
|
boost += Gamepad.current.leftTrigger.ReadValue() * 0.2f;
|
|
}
|
|
else
|
|
{
|
|
boost = 2f;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
boost += Input.mouseScrollDelta.y * 0.2f;
|
|
}
|
|
translation *= Mathf.Pow(2f, boost);
|
|
m_TargetCameraState.Translate(translation);
|
|
float positionLerpPct = 1f - Mathf.Exp(Mathf.Log(0.00999999f) / positionLerpTime * Time.deltaTime);
|
|
float rotationLerpPct = 1f - Mathf.Exp(Mathf.Log(0.00999999f) / rotationLerpTime * Time.deltaTime);
|
|
m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);
|
|
m_InterpolatingCameraState.UpdateTransform(base.transform);
|
|
}
|
|
}
|
|
}
|