forked from HoneyFox/KerbTown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKtCamera.cs
134 lines (108 loc) · 4.79 KB
/
KtCamera.cs
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
/* LICENSE
* This source code is copyrighted.
* All rights reserved.
* Copyright © Ryan Irecki 2013
*/
using UnityEngine;
namespace Kerbtown
{
public partial class KerbTown
{
public float CameraDistance = 50.0f;
private float _cameraX;
private float _cameraY;
// Rather than adding a script to the FlightCamera, we'll just control it natively from KerbTown.
private void LateUpdate()
{
if (!KtCamera.ManualControl)
return;
if (Input.GetMouseButton(1)) // RMB
{
_cameraX += Input.GetAxis("Mouse X")*KtCamera.CameraSpeed;
_cameraY -= Input.GetAxis("Mouse Y")*KtCamera.CameraSpeed;
}
if (GameSettings.AXIS_MOUSEWHEEL.GetAxis() != 0f) // MMB
{
CameraDistance =
Mathf.Clamp(
CameraDistance*
(1f - (GameSettings.AXIS_MOUSEWHEEL.GetAxis()*KtCamera.ActiveFlightCamera.zoomScaleFactor)),
KtCamera.ActiveFlightCamera.minDistance, KtCamera.ActiveFlightCamera.maxDistance);
}
if (Mouse.Right.GetDoubleClick())
{
_cameraX = 0;
_cameraY = 0;
}
KtCamera.ActiveCameraPivot.transform.rotation = KtCamera.ActiveCameraPivot.transform.parent.rotation;
KtCamera.ActiveCameraPivot.transform.RotateAround(KtCamera.ActiveCameraPivot.transform.up, _cameraX);
KtCamera.ActiveCameraPivot.transform.RotateAround(KtCamera.ActiveCameraPivot.transform.right, _cameraY);
KtCamera.ActiveFlightCamera.transform.localPosition =
Vector3.Lerp(
KtCamera.ActiveFlightCamera.transform.localPosition,
Vector3.back*CameraDistance,
KtCamera.ActiveFlightCamera.sharpness*Time.deltaTime);
KtCamera.ActiveFlightCamera.transform.localRotation =
Quaternion.LookRotation(-KtCamera.ActiveFlightCamera.transform.localPosition, Vector3.up);
}
}
public static class KtCamera
{
private static Transform _originalParentTransform;
private static bool _manualControl;
public static FlightCamera ActiveFlightCamera;
public static GameObject ActiveCameraPivot;
public static float CameraSpeed = 0f;
//public static Transform Target;
public static bool ManualControl
{
set
{
if (value && ActiveFlightCamera == null)
{
_manualControl = false;
Extensions.LogError("Tried to set manual camera control while FlightCamera.fetch was null.");
return;
}
_manualControl = value;
}
get { return _manualControl; }
}
public static void SetCameraParent(Transform parentTransform)
{
// Assign FlightCamera instance to public var.
ActiveFlightCamera = FlightCamera.fetch;
// For replacing the camera when done editing.
if (_originalParentTransform == null)
_originalParentTransform = ActiveFlightCamera.transform.parent;
// For translating the camera
if (ActiveCameraPivot != null) Object.Destroy(ActiveCameraPivot);
ActiveCameraPivot = new GameObject("KtCamPivot");
ActiveCameraPivot.transform.parent = parentTransform;
ActiveCameraPivot.transform.localPosition = Vector3.zero;
ActiveCameraPivot.transform.localRotation = Quaternion.identity;
// Switch to active object.
ActiveFlightCamera.transform.parent = ActiveCameraPivot.transform;
// Use the FlightCamera sensitivity for the speed.
CameraSpeed = ActiveFlightCamera.orbitSensitivity;
// Take control of the flight camera.
ActiveFlightCamera.DeactivateUpdate();
// Instruct LateUpdate that we're controlling the camera manually now.
ManualControl = true;
// Say something.
Extensions.PostScreenMessage("[KerbTown] FlightCamera switched to: " + parentTransform.name);
}
public static void RestoreCameraParent()
{
// Only execute if we're actually controlling the camera.
if (!ManualControl) return;
// Restore camera control to vessel.
FlightCamera.fetch.transform.parent = _originalParentTransform;
_originalParentTransform = null;
ManualControl = false;
ActiveFlightCamera.ActivateUpdate();
// Say something.
Extensions.PostScreenMessage("[KerbTown] FlightCamera restored to vessel.");
}
}
}