-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTSCamera.cs
More file actions
122 lines (105 loc) · 4.19 KB
/
RTSCamera.cs
File metadata and controls
122 lines (105 loc) · 4.19 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2014, Roger Lew (rogerlew.gmail.com)
* Date: 5/12/2015
* License: BSD (3-clause license)
*
* The project described was supported by NSF award number IIA-1301792
* from the NSF Idaho EPSCoR Program and by the National Science Foundation.
*
*/
using UnityEngine;
using System.Collections;
namespace VTL.RTSCamera
{
public class RTSCamera : MonoBehaviour
{
// WASDQE Panning
public float minPanSpeed = 0.1f; // Starting panning speed
public float maxPanSpeed = 1000f; // Max panning speed
public float panTimeConstant = 20f; // Time to reach max panning speed
// Mouse right-down rotation
public float rotateSpeed = 10; // mouse down rotation speed about x and y axes
public float zoomSpeed = 2; // zoom speed
float panT = 0;
float panSpeed = 10;
Vector3 panTranslation;
bool wKeyDown = false;
bool aKeyDown = false;
bool sKeyDown = false;
bool dKeyDown = false;
bool qKeyDown = false;
bool eKeyDown = false;
Vector3 lastMousePosition;
new Camera camera;
void Start()
{
camera = GetComponent<Camera>();
}
void Update()
{
//
// WASDQE Panning
// read key inputs
wKeyDown = Input.GetKey(KeyCode.W);
aKeyDown = Input.GetKey(KeyCode.A);
sKeyDown = Input.GetKey(KeyCode.S);
dKeyDown = Input.GetKey(KeyCode.D);
qKeyDown = Input.GetKey(KeyCode.Q);
eKeyDown = Input.GetKey(KeyCode.E);
// determine panTranslation
panTranslation = Vector3.zero;
if (dKeyDown && !aKeyDown)
panTranslation += Vector3.right * Time.deltaTime * panSpeed;
else if (aKeyDown && !dKeyDown)
panTranslation += Vector3.left * Time.deltaTime * panSpeed;
if (wKeyDown && !sKeyDown)
panTranslation += Vector3.forward * Time.deltaTime * panSpeed;
else if (sKeyDown && !wKeyDown)
panTranslation += Vector3.back * Time.deltaTime * panSpeed;
if (qKeyDown && !eKeyDown)
panTranslation += Vector3.down * Time.deltaTime * panSpeed;
else if (eKeyDown && !qKeyDown)
panTranslation += Vector3.up * Time.deltaTime * panSpeed;
transform.Translate(panTranslation, Space.Self);
// Update panSpeed
if (wKeyDown || aKeyDown || sKeyDown ||
dKeyDown || qKeyDown || eKeyDown)
{
panT += Time.deltaTime / panTimeConstant;
panSpeed = Mathf.Lerp(minPanSpeed, maxPanSpeed, panT * panT);
}
else
{
panT = 0;
panSpeed = minPanSpeed;
}
//
// Mouse Rotation
if (Input.GetMouseButton(1))
{
// if the game window is separate from the editor window and the editor
// window is active then you go to right-click on the game window the
// rotation jumps if we don't ignore the mouseDelta for that frame.
Vector3 mouseDelta;
if (lastMousePosition.x >= 0 &&
lastMousePosition.y >= 0 &&
lastMousePosition.x <= Screen.width &&
lastMousePosition.y <= Screen.height)
mouseDelta = Input.mousePosition - lastMousePosition;
else
mouseDelta = Vector3.zero;
var rotation = Vector3.up * Time.deltaTime * rotateSpeed * mouseDelta.x;
rotation += Vector3.left * Time.deltaTime * rotateSpeed * mouseDelta.y;
transform.Rotate(rotation, Space.Self);
// Make sure z rotation stays locked
rotation = transform.rotation.eulerAngles;
rotation.z = 0;
transform.rotation = Quaternion.Euler(rotation);
}
lastMousePosition = Input.mousePosition;
//
// Mouse Zoom
camera.fieldOfView -= Input.mouseScrollDelta.y * zoomSpeed;
}
}
}