1+ using UnityEngine;
2+ using System.Collections;
3+
4+ public class FlyCamera : MonoBehaviour
5+ {
6+
7+ /*
8+ Writen by Windexglow 11-13-10. Use it, edit it, steal it I don't care.
9+ Converted to C# 27-02-13 - no credit wanted.
10+ Simple flycam I made, since I couldn't find any others made public.
11+ Made simple to use (drag and drop, done) for regular keyboard layout
12+ wasd : basic movement
13+ shift : Makes camera accelerate
14+ space : Moves camera on X and Z axis only. So camera doesn't gain any height*/
15+
16+
17+ float mainSpeed = 100.0f; //regular speed
18+ float shiftAdd = 250.0f; //multiplied by how long shift is held. Basically running
19+ float maxShift = 1000.0f; //Maximum speed when holdin gshift
20+ float camSens = 0.25f; //How sensitive it with mouse
21+ private Vector3 lastMouse = new Vector3(255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
22+ private float totalRun = 1.0f;
23+
24+ void Update()
25+ {
26+ lastMouse = Input.mousePosition - lastMouse;
27+ lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
28+ lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
29+ transform.eulerAngles = lastMouse;
30+ lastMouse = Input.mousePosition;
31+ //Mouse camera angle done.
32+
33+ //Keyboard commands
34+ float f = 0.0f;
35+ Vector3 p = GetBaseInput();
36+ if (p.sqrMagnitude > 0)
37+ { // only move while a direction key is pressed
38+ if (Input.GetKey(KeyCode.LeftShift))
39+ {
40+ totalRun += Time.deltaTime;
41+ p = p * totalRun * shiftAdd;
42+ p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
43+ p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
44+ p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
45+ }
46+ else
47+ {
48+ totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
49+ p = p * mainSpeed;
50+ }
51+
52+ p = p * Time.deltaTime;
53+ Vector3 newPosition = transform.position;
54+ if (Input.GetKey(KeyCode.Space))
55+ { //If player wants to move on X and Z axis only
56+ transform.Translate(p);
57+ newPosition.x = transform.position.x;
58+ newPosition.z = transform.position.z;
59+ transform.position = newPosition;
60+ }
61+ else
62+ {
63+ transform.Translate(p);
64+ }
65+ }
66+ }
67+
68+ private Vector3 GetBaseInput()
69+ { //returns the basic values, if it's 0 than it's not active.
70+ Vector3 p_Velocity = new Vector3();
71+ if (Input.GetKey(KeyCode.W))
72+ {
73+ p_Velocity += new Vector3(0, 0, 1);
74+ }
75+ if (Input.GetKey(KeyCode.S))
76+ {
77+ p_Velocity += new Vector3(0, 0, -1);
78+ }
79+ if (Input.GetKey(KeyCode.A))
80+ {
81+ p_Velocity += new Vector3(-1, 0, 0);
82+ }
83+ if (Input.GetKey(KeyCode.D))
84+ {
85+ p_Velocity += new Vector3(1, 0, 0);
86+ }
87+ return p_Velocity;
88+ }
89+ }
0 commit comments