Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class FreeFlightCamera : MonoBehaviour
- {
- public static readonly string[] Description = new string[]
- {
- "WASD to move",
- "RMB to rotate",
- "Space, Ctrl to jump",
- "Shift to accelerate"
- };
- public bool DrawInfo = true;
- public float LiniarSpeed = 10;
- public float AngularRotationArc = 130;
- private Vector3 _lastMousePosition;
- private void SimulateMotion(float deltaTime)
- {
- var motion = Vector3.zero;
- var jump = Vector3.zero;
- if (Input.GetKey(KeyCode.W)) motion.z = 1;
- if (Input.GetKey(KeyCode.S)) motion.z = -1;
- if (Input.GetKey(KeyCode.D)) motion.x = 1;
- if (Input.GetKey(KeyCode.A)) motion.x = -1;
- if (Input.GetKey(KeyCode.Space)) jump.y = 1;
- if (Input.GetKey(KeyCode.LeftControl)) jump.y = -1;
- motion = deltaTime * LiniarSpeed * motion.normalized;
- jump = deltaTime * LiniarSpeed * jump;
- if (Input.GetKey(KeyCode.LeftShift))
- {
- motion *= 5;
- jump *= 5;
- }
- transform.Translate(motion);
- transform.Translate(jump, Space.World);
- }
- private void SimulateRotation()
- {
- if (Input.GetMouseButtonDown(1))
- _lastMousePosition = Input.mousePosition;
- if (Input.GetMouseButton(1))
- {
- var mouseDelta = Input.mousePosition - _lastMousePosition;
- var x = Mathf.Abs(mouseDelta.x / Screen.width) * AngularRotationArc * Mathf.Sign(mouseDelta.x);
- var y = -Mathf.Abs(mouseDelta.y / Screen.height) * AngularRotationArc * Mathf.Sign(mouseDelta.y);
- transform.Rotate(Vector3.right, y);
- transform.Rotate(0, x, 0, Space.World);
- _lastMousePosition = Input.mousePosition;
- }
- }
- private void Update()
- {
- SimulateMotion(Time.deltaTime);
- SimulateRotation();
- }
- private void OnGUI()
- {
- if (DrawInfo) DrawTextLines(Description);
- }
- private void DrawTextLines(string[] lines)
- {
- var lineHeight = 24;
- var lineWidth = 256;
- for(var i = 0; i < lines.Length; i++)
- GUI.TextField(new Rect(0, lineHeight * i, lineWidth, lineHeight), lines[i]);
- }
- }
Add Comment
Please, Sign In to add comment