This repository has been archived on 2024-12-03. You can view files and clone it, but cannot push or open issues or pull requests.

92 lines
3.3 KiB
C#
Raw Normal View History

2023-02-18 21:35:18 +00:00
using JetBrains.Annotations;
2022-06-03 14:28:35 +03:00
using UnityEngine;
2021-06-02 23:05:01 +03:00
namespace Terkoiz.Freecam
{
/// <summary>
/// A simple free camera to be added to a Unity game object.
///
2024-07-09 00:06:17 +03:00
/// Full credit to Ashley Davis on GitHub for the initial code:
2021-06-02 23:05:01 +03:00
/// https://gist.github.com/ashleydavis/f025c03a9221bc840a2b
///
/// </summary>
public class Freecam : MonoBehaviour
{
public bool IsActive = false;
2023-02-18 20:23:20 +00:00
2023-02-18 21:35:18 +00:00
[UsedImplicitly]
2022-06-03 14:28:35 +03:00
public void Update()
2021-06-02 23:05:01 +03:00
{
if (!IsActive)
{
return;
}
2021-06-02 23:05:01 +03:00
var fastMode = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
var movementSpeed = fastMode ? FreecamPlugin.CameraFastMoveSpeed.Value : FreecamPlugin.CameraMoveSpeed.Value;
2021-06-02 23:05:01 +03:00
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
2023-02-18 20:23:20 +00:00
transform.position += (-transform.right * (movementSpeed * Time.deltaTime));
2021-06-02 23:05:01 +03:00
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
2023-02-18 20:23:20 +00:00
transform.position += (transform.right * (movementSpeed * Time.deltaTime));
2021-06-02 23:05:01 +03:00
}
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
2023-02-18 20:23:20 +00:00
transform.position += (transform.forward * (movementSpeed * Time.deltaTime));
2021-06-02 23:05:01 +03:00
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
2023-02-18 20:23:20 +00:00
transform.position += (-transform.forward * (movementSpeed * Time.deltaTime));
2021-06-02 23:05:01 +03:00
}
2022-06-03 14:28:35 +03:00
if (FreecamPlugin.CameraHeightMovement.Value)
2021-06-02 23:05:01 +03:00
{
2022-06-03 14:28:35 +03:00
if (Input.GetKey(KeyCode.Q))
{
2023-02-18 20:23:20 +00:00
transform.position += (transform.up * (movementSpeed * Time.deltaTime));
2022-06-03 14:28:35 +03:00
}
2021-06-02 23:05:01 +03:00
2022-06-03 14:28:35 +03:00
if (Input.GetKey(KeyCode.E))
{
2023-02-18 20:23:20 +00:00
transform.position += (-transform.up * (movementSpeed * Time.deltaTime));
2022-06-03 14:28:35 +03:00
}
2021-06-02 23:05:01 +03:00
2022-06-03 14:28:35 +03:00
if (Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.PageUp))
{
2023-02-18 20:23:20 +00:00
transform.position += (Vector3.up * (movementSpeed * Time.deltaTime));
2022-06-03 14:28:35 +03:00
}
2021-06-02 23:05:01 +03:00
2022-06-03 14:28:35 +03:00
if (Input.GetKey(KeyCode.F) || Input.GetKey(KeyCode.PageDown))
{
2023-02-18 20:23:20 +00:00
transform.position += (-Vector3.up * (movementSpeed * Time.deltaTime));
2022-06-03 14:28:35 +03:00
}
2021-06-02 23:05:01 +03:00
}
float newRotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * FreecamPlugin.CameraLookSensitivity.Value;
float newRotationY = transform.localEulerAngles.x - Input.GetAxis("Mouse Y") * FreecamPlugin.CameraLookSensitivity.Value;
2021-06-02 23:05:01 +03:00
transform.localEulerAngles = new Vector3(newRotationY, newRotationX, 0f);
2022-06-03 14:28:35 +03:00
if (FreecamPlugin.CameraMousewheelZoom.Value)
2021-06-02 23:05:01 +03:00
{
2022-06-03 14:28:35 +03:00
float axis = Input.GetAxis("Mouse ScrollWheel");
if (axis != 0)
{
var zoomSensitivity = fastMode ? FreecamPlugin.CameraFastZoomSpeed.Value : FreecamPlugin.CameraZoomSpeed.Value;
2023-02-18 20:23:20 +00:00
transform.position += transform.forward * (axis * zoomSensitivity);
2022-06-03 14:28:35 +03:00
}
2021-06-02 23:05:01 +03:00
}
}
2023-02-18 20:23:20 +00:00
2023-02-18 21:35:18 +00:00
[UsedImplicitly]
2023-02-18 20:23:20 +00:00
private void OnDestroy()
{
Destroy(this);
}
2021-06-02 23:05:01 +03:00
}
}