Tactics /New Unity Project/Assets/Javascript Scripts/CameraControl.js

Language Javascript Lines 169
MD5 Hash 8c735547fe725dd7c09daee82685bc6a
Repository https://github.com/TacticsMMO/Tactics.git View Raw File
| Open JSFiddle
  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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma strict
 
public var TargetLookAt : Transform;
 
public var Distance : float = 330.0;
public var DistanceMin : float = 10.0;
public var DistanceMax : float = 1000.0;  
private var mouseX : float = 0.0;
private var mouseY : float = 0.0;
private var startingDistance : float = 330.0; 
private var desiredDistance : float = 330.0;  
public var X_MouseSensitivity : float = 5.0;
public var Y_MouseSensitivity : float = 5.0;
public var MouseWheelSensitivity : float = 100.0;
public var Y_MinLimit : float = 0.0;
public var Y_MaxLimit : float = 330.0;   
public var DistanceSmooth : float = 0.05;   
private var velocityDistance : float = 0.0; 
private var desiredPosition : Vector3 = Vector3.zero;   
public var X_Smooth : float = 0.05;
public var Y_Smooth : float = 0.1;
private var velX : float = 0.0;
private var velY : float = 0.0;
private var velZ : float = 0.0;
private var position : Vector3 = Vector3.zero;

// map limits for wasd
public var map_minX : float = 20;
public var map_maxX : float = 284;
public var map_minZ : float = 10;
public var map_maxZ : float = 280;
public var cur_X : float;
public var cur_Z : float;
public var moveSpeed : float = 10;
public var FocusPoint : GameObject;
public var cameraVector : Vector3;

 
 
function Start() 
{
	    
    FocusPoint = GameObject.FindWithTag("Focus");
    cur_X = FocusPoint.transform.position.x;
    cur_Z = FocusPoint.transform.position.z;
    Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax);
    startingDistance = Distance;
    Reset();
}
 
function LateUpdate()
{
    if (TargetLookAt == null)
       return;
 
    HandlePlayerInput();
 
    CalculateDesiredPosition();
 
    UpdatePosition();
}
 
function HandlePlayerInput()
{
    var deadZone = 0.01; // mousewheel deadZone
 
    if (Input.GetMouseButton(1))
    {
       mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
       mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
    }
 
    // this is where the mouseY is limited - Helper script
    mouseY = ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit);
 
    // get Mouse Wheel Input
    if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone)
    {
       desiredDistance = Mathf.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity), 
                                                 DistanceMin, DistanceMax);
    }
    
    
    
    if (Input.GetKey(KeyCode.W)){
    	cameraVector = FocusPoint.transform.position + Camera.mainCamera.transform.forward;
    	if (cameraVector.x > map_minX && cameraVector.x < map_maxX && cameraVector.z > map_minZ && cameraVector.z < map_maxZ){
    		FocusPoint.transform.position += Camera.mainCamera.transform.forward;
    		cur_X = FocusPoint.transform.position.x;
    		cur_Z = FocusPoint.transform.position.z;
    		}
    } 
    
    if (Input.GetKey(KeyCode.S)){
    	cameraVector = FocusPoint.transform.position - Camera.mainCamera.transform.forward;
    	if (cameraVector.x > map_minX && cameraVector.x < map_maxX && cameraVector.z > map_minZ && cameraVector.z < map_maxZ){
    		FocusPoint.transform.position -= Camera.mainCamera.transform.forward;
    		cur_X = FocusPoint.transform.position.x;
    		cur_Z = FocusPoint.transform.position.z;
    		}
    }
    
    if (Input.GetKey(KeyCode.A)){
    	cameraVector = FocusPoint.transform.position - Camera.mainCamera.transform.right;
    	if (cameraVector.x > map_minX && cameraVector.x < map_maxX && cameraVector.z > map_minZ && cameraVector.z < map_maxZ){
    		FocusPoint.transform.position -= Camera.mainCamera.transform.right;
    		cur_Z = FocusPoint.transform.position.z;
    		cur_X = FocusPoint.transform.position.x;
    		}
    }
    
    if (Input.GetKey(KeyCode.D)){
    	cameraVector = FocusPoint.transform.position + Camera.mainCamera.transform.right;
    	if (cameraVector.x > map_minX && cameraVector.x < map_maxX && cameraVector.z > map_minZ && cameraVector.z < map_maxZ){
    		FocusPoint.transform.position += Camera.mainCamera.transform.right;
    		cur_Z = FocusPoint.transform.position.z;
    		cur_X = FocusPoint.transform.position.x;
    		}
    }
}
 
function CalculateDesiredPosition()
{
    // Evaluate distance
    Distance = Mathf.SmoothDamp(Distance, desiredDistance, velocityDistance, DistanceSmooth);
 
    // Calculate desired position -> Note : mouse inputs reversed to align to WorldSpace Axis
    desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
}
 
function CalculatePosition(rotationX : float, rotationY : float, distance : float)
{
    var direction : Vector3 = Vector3(0, 0, -distance);
    var rotation : Quaternion = Quaternion.Euler(rotationX, rotationY, 0);
    return TargetLookAt.position + (rotation * direction);
}
 
function UpdatePosition()
{
    var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, velX, X_Smooth);
    var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, velY, Y_Smooth);
    var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, velZ, X_Smooth);
    position = Vector3(posX, posY, posZ);
 
    transform.position = position;
 
    transform.LookAt(TargetLookAt);
}
 
function Reset() 
{
    mouseX = 0;
    mouseY = 10;
    Distance = startingDistance;
    desiredDistance = Distance;
}
 
function ClampAngle(angle : float, min : float, max : float)
{
    while (angle < -360 || angle > 360)
    {
       if (angle < -360)
         angle += 360;
       if (angle > 360)
         angle -= 360;
    }
 
    return Mathf.Clamp(angle, min, max);
}
Back to Top