1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Camera))] public class CameraMoveController : MonoBehaviour { public float speed = 4.0f; public float shiftSpeed = 16.0f; public bool showInstructions = true;
private Vector3 startEulerAngles; private Vector3 startMousePosition; private float realTime;
void OnEnable() { realTime = Time.realtimeSinceStartup; }
void Update() { float forward = 0.0f; if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { forward += 1.0f; }
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) { forward -= 1.0f; }
float up = 0.0f; if (Input.GetKey(KeyCode.E)) { up += 1.0f; }
if (Input.GetKey(KeyCode.Q)) { up -= 1.0f; }
float right = 0.0f; if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { right += 1.0f; }
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { right -= 1.0f; }
float currentSpeed = speed; if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { currentSpeed = shiftSpeed; }
float realTimeNow = Time.realtimeSinceStartup; float deltaRealTime = realTimeNow - realTime; realTime = realTimeNow;
Vector3 delta = new Vector3(right, up, forward) * currentSpeed * deltaRealTime;
transform.position += transform.TransformDirection(delta);
Vector3 mousePosition = Input.mousePosition;
if (Input.GetMouseButtonDown(1)) { startMousePosition = mousePosition; startEulerAngles = transform.localEulerAngles; }
if (Input.GetMouseButton(1)) { Vector3 offset = mousePosition - startMousePosition; transform.localEulerAngles = startEulerAngles + new Vector3(-offset.y * 360.0f / Screen.height, offset.x * 360.0f / Screen.width, 0.0f); } }
void OnGUI() { if (showInstructions) { GUI.Label(new Rect(10.0f, 10.0f, 600.0f, 400.0f), "WASD 前后左右移动相机\n " + "EQ 上升、降低相机高度\n" + "鼠标右键旋转相机\n"); } }
}
|