diff options
| author | Alee <Alee14498@gmail.com> | 2019-05-31 19:38:47 -0400 |
|---|---|---|
| committer | Alee <Alee14498@gmail.com> | 2019-05-31 19:38:47 -0400 |
| commit | ac5d2f813801933f7044c0fcfd15a6d79df78958 (patch) | |
| tree | 3e5c61e1fa53c6b8b3fcea4a2b192c91ac811ecd | |
| parent | a65596aab8f89a2921574c216a27e57152ab1a2b (diff) | |
| download | Unicity-ac5d2f813801933f7044c0fcfd15a6d79df78958.tar.gz Unicity-ac5d2f813801933f7044c0fcfd15a6d79df78958.tar.bz2 Unicity-ac5d2f813801933f7044c0fcfd15a6d79df78958.zip | |
Camera is now working
| -rw-r--r-- | Assets/RTS_Camera.meta | 8 | ||||
| -rw-r--r-- | Assets/RTS_Camera/RTS_Camera.prefab | 175 | ||||
| -rw-r--r-- | Assets/RTS_Camera/RTS_Camera.prefab.meta | 8 | ||||
| -rw-r--r-- | Assets/RTS_Camera/Scripts.meta | 9 | ||||
| -rw-r--r-- | Assets/RTS_Camera/Scripts/Editor.meta | 9 | ||||
| -rw-r--r-- | Assets/RTS_Camera/Scripts/Editor/LayoutHelpers.cs | 108 | ||||
| -rw-r--r-- | Assets/RTS_Camera/Scripts/Editor/LayoutHelpers.cs.meta | 12 | ||||
| -rw-r--r-- | Assets/RTS_Camera/Scripts/Editor/RTS_CameraEditor.cs | 158 | ||||
| -rw-r--r-- | Assets/RTS_Camera/Scripts/Editor/RTS_CameraEditor.cs.meta | 12 | ||||
| -rw-r--r-- | Assets/RTS_Camera/Scripts/RTS_Camera.cs | 344 | ||||
| -rw-r--r-- | Assets/RTS_Camera/Scripts/RTS_Camera.cs.meta | 12 | ||||
| -rw-r--r-- | Assets/Scenes/TestSimulation.unity | 69 | ||||
| -rw-r--r-- | Assets/Scripts/Player/PlayerMovement.cs | 2 |
13 files changed, 919 insertions, 7 deletions
diff --git a/Assets/RTS_Camera.meta b/Assets/RTS_Camera.meta new file mode 100644 index 0000000..7484454 --- /dev/null +++ b/Assets/RTS_Camera.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4e328816a8e913e4b870da94ede9faed +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RTS_Camera/RTS_Camera.prefab b/Assets/RTS_Camera/RTS_Camera.prefab new file mode 100644 index 0000000..628ddc8 --- /dev/null +++ b/Assets/RTS_Camera/RTS_Camera.prefab @@ -0,0 +1,175 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &120296 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 459968} + - component: {fileID: 2056488} + - component: {fileID: 9275010} + - component: {fileID: 12455804} + - component: {fileID: 8165074} + - component: {fileID: 11488424} + - component: {fileID: 11455738} + m_Layer: 0 + m_Name: RTS_Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &459968 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 120296} + m_LocalRotation: {x: 0.7071069, y: 0, z: 0, w: 0.70710677} + m_LocalPosition: {x: 0, y: 6.75, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!20 &2056488 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 120296} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0.019607844} + m_projectionMatrixMode: 1 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_GateFitMode: 2 + m_FocalLength: 50 + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 0 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 0 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!92 &9275010 +Behaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 120296} + m_Enabled: 1 +--- !u!124 &12455804 +Behaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 120296} + m_Enabled: 1 +--- !u!81 &8165074 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 120296} + m_Enabled: 1 +--- !u!114 &11488424 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 120296} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 20048c13a5d194941bac6cd566a5e468, type: 3} + m_Name: + m_EditorClassIdentifier: + targetsTag: target +--- !u!114 &11455738 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 120296} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 28ef8c68bade09b41aca258d42a632f2, type: 3} + m_Name: + m_EditorClassIdentifier: + lastTab: 0 + movementSettingsFoldout: 1 + zoomingSettingsFoldout: 0 + rotationSettingsFoldout: 1 + heightSettingsFoldout: 1 + mapLimitSettingsFoldout: 1 + targetingSettingsFoldout: 1 + inputSettingsFoldout: 1 + useFixedUpdate: 0 + keyboardMovementSpeed: 5 + screenEdgeMovementSpeed: 3 + followingSpeed: 5 + rotationSped: 3 + panningSpeed: 25 + mouseRotationSpeed: 100 + autoHeight: 1 + groundMask: + serializedVersion: 2 + m_Bits: 55 + maxHeight: 15 + minHeight: 10 + heightDampening: 5 + keyboardZoomingSensitivity: 2 + scrollWheelZoomingSensitivity: 25 + limitMap: 1 + limitX: 10 + limitY: 10 + targetFollow: {fileID: 0} + targetOffset: {x: 0, y: 0, z: 0} + useScreenEdgeInput: 1 + screenEdgeBorder: 25 + useKeyboardInput: 1 + horizontalAxis: Horizontal + verticalAxis: Vertical + usePanning: 1 + panningKey: 325 + useKeyboardZooming: 1 + zoomInKey: 101 + zoomOutKey: 113 + useScrollwheelZooming: 1 + zoomingAxis: Mouse ScrollWheel + useKeyboardRotation: 1 + rotateRightKey: 120 + rotateLeftKey: 122 + useMouseRotation: 1 + mouseRotationKey: 324 diff --git a/Assets/RTS_Camera/RTS_Camera.prefab.meta b/Assets/RTS_Camera/RTS_Camera.prefab.meta new file mode 100644 index 0000000..f5bb07f --- /dev/null +++ b/Assets/RTS_Camera/RTS_Camera.prefab.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 407bd5cbe8c43334bb1b25c0f3826974 +timeCreated: 1440059567 +licenseType: Store +NativeFormatImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RTS_Camera/Scripts.meta b/Assets/RTS_Camera/Scripts.meta new file mode 100644 index 0000000..dd00394 --- /dev/null +++ b/Assets/RTS_Camera/Scripts.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 0093e3f39000d9b47a825023d8a01b0e +folderAsset: yes +timeCreated: 1438769848 +licenseType: Store +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RTS_Camera/Scripts/Editor.meta b/Assets/RTS_Camera/Scripts/Editor.meta new file mode 100644 index 0000000..e25e69e --- /dev/null +++ b/Assets/RTS_Camera/Scripts/Editor.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: e5e4a28cbe8c55244bb1b72b33e5f8b6 +folderAsset: yes +timeCreated: 1438769856 +licenseType: Store +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RTS_Camera/Scripts/Editor/LayoutHelpers.cs b/Assets/RTS_Camera/Scripts/Editor/LayoutHelpers.cs new file mode 100644 index 0000000..2c41a09 --- /dev/null +++ b/Assets/RTS_Camera/Scripts/Editor/LayoutHelpers.cs @@ -0,0 +1,108 @@ +using UnityEngine; +using System; +using System.Collections.Generic; +using UnityEditor; +using System.Linq; + + +public class VerticalBlock : IDisposable +{ + public VerticalBlock(params GUILayoutOption[] options) + { + GUILayout.BeginVertical(options); + } + + public VerticalBlock(GUIStyle style, params GUILayoutOption[] options) + { + GUILayout.BeginVertical(style, options); + } + + public void Dispose() + { + GUILayout.EndVertical(); + } +} + +public class ScrollviewBlock : IDisposable +{ + public ScrollviewBlock(ref Vector2 scrollPos, params GUILayoutOption[] options) + { + scrollPos = GUILayout.BeginScrollView(scrollPos, options); + } + + public void Dispose() + { + GUILayout.EndScrollView(); + } +} + +public class HorizontalBlock : IDisposable +{ + public HorizontalBlock(params GUILayoutOption[] options) + { + GUILayout.BeginHorizontal(options); + } + + public HorizontalBlock(GUIStyle style, params GUILayoutOption[] options) + { + GUILayout.BeginHorizontal(style, options); + } + + public void Dispose() + { + GUILayout.EndHorizontal(); + } +} + +public class ColoredBlock : System.IDisposable +{ + public ColoredBlock(Color color) + { + GUI.color = color; + } + + public void Dispose() + { + GUI.color = Color.white; + } +} + +[Serializable] +public class TabsBlock +{ + private Dictionary<string, Action> methods; + private Action currentGuiMethod; + public int curMethodIndex = -1; + + public TabsBlock(Dictionary<string, Action> _methods) + { + methods = _methods; + SetCurrentMethod(0); + } + + public void Draw() + { + var keys = methods.Keys.ToArray(); + using (new VerticalBlock(EditorStyles.helpBox)) + { + using (new HorizontalBlock()) + { + for (int i = 0; i < keys.Length; i++) + { + var btnStyle = i == 0 ? EditorStyles.miniButtonLeft : i == (keys.Length - 1) ? EditorStyles.miniButtonRight : EditorStyles.miniButtonMid; + using (new ColoredBlock(currentGuiMethod == methods[keys[i]] ? Color.grey : Color.white)) + if (GUILayout.Button(keys[i], btnStyle)) + SetCurrentMethod(i); + } + } + GUILayout.Label(keys[curMethodIndex], EditorStyles.centeredGreyMiniLabel); + currentGuiMethod(); + } + } + + public void SetCurrentMethod(int index) + { + curMethodIndex = index; + currentGuiMethod = methods[methods.Keys.ToArray()[index]]; + } +}
\ No newline at end of file diff --git a/Assets/RTS_Camera/Scripts/Editor/LayoutHelpers.cs.meta b/Assets/RTS_Camera/Scripts/Editor/LayoutHelpers.cs.meta new file mode 100644 index 0000000..07148cb --- /dev/null +++ b/Assets/RTS_Camera/Scripts/Editor/LayoutHelpers.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: e6604c54827307c4083b63203d4fed4b +timeCreated: 1451938860 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RTS_Camera/Scripts/Editor/RTS_CameraEditor.cs b/Assets/RTS_Camera/Scripts/Editor/RTS_CameraEditor.cs new file mode 100644 index 0000000..d4a828f --- /dev/null +++ b/Assets/RTS_Camera/Scripts/Editor/RTS_CameraEditor.cs @@ -0,0 +1,158 @@ +using UnityEngine; +using System.Collections.Generic; +using UnityEditor; + +namespace RTS_Cam +{ + [CustomEditor(typeof(RTS_Camera))] + public class RTS_CameraEditor : Editor + { + private RTS_Camera camera { get { return target as RTS_Camera; } } + + private TabsBlock tabs; + + private void OnEnable() + { + tabs = new TabsBlock(new Dictionary<string, System.Action>() + { + {"Movement", MovementTab}, + {"Rotation", RotationTab}, + {"Height", HeightTab} + }); + tabs.SetCurrentMethod(camera.lastTab); + } + + public override void OnInspectorGUI() + { + //base.OnInspectorGUI(); + Undo.RecordObject(camera, "RTS_CAmera"); + tabs.Draw(); + if (GUI.changed) + camera.lastTab = tabs.curMethodIndex; + EditorUtility.SetDirty(camera); + } + + private void MovementTab() + { + using (new HorizontalBlock()) + { + GUILayout.Label("Use keyboard input: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.useKeyboardInput = EditorGUILayout.Toggle( camera.useKeyboardInput); + } + if(camera.useKeyboardInput) + { + camera.horizontalAxis = EditorGUILayout.TextField("Horizontal axis name: ", camera.horizontalAxis); + camera.verticalAxis = EditorGUILayout.TextField("Vertical axis name: ", camera.verticalAxis); + camera.keyboardMovementSpeed = EditorGUILayout.FloatField("Movement speed: ", camera.keyboardMovementSpeed); + } + + using (new HorizontalBlock()) + { + GUILayout.Label("Screen edge input: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.useScreenEdgeInput = EditorGUILayout.Toggle( camera.useScreenEdgeInput); + } + + if(camera.useScreenEdgeInput) + { + EditorGUILayout.FloatField("Screen edge border size: ", camera.screenEdgeBorder); + camera.screenEdgeMovementSpeed = EditorGUILayout.FloatField("Screen edge movement speed: ", camera.screenEdgeMovementSpeed); + } + + using (new HorizontalBlock()) + { + GUILayout.Label("Panning with mouse: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.usePanning = EditorGUILayout.Toggle(camera.usePanning); + } + if(camera.usePanning) + { + camera.panningKey = (KeyCode)EditorGUILayout.EnumPopup("Panning when holding: ", camera.panningKey); + camera.panningSpeed = EditorGUILayout.FloatField("Panning speed: ", camera.panningSpeed); + } + + using (new HorizontalBlock()) + { + GUILayout.Label("Limit movement: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.limitMap = EditorGUILayout.Toggle(camera.limitMap); + } + if (camera.limitMap) + { + camera.limitX = EditorGUILayout.FloatField("Limit X: ", camera.limitX); + camera.limitY = EditorGUILayout.FloatField("Limit Y: ", camera.limitY); + } + + GUILayout.Label("Follow target", EditorStyles.boldLabel); + camera.targetFollow = EditorGUILayout.ObjectField("Target to follow: ", camera.targetFollow, typeof(Transform)) as Transform; + camera.targetOffset = EditorGUILayout.Vector3Field("Target offset: ", camera.targetOffset); + camera.followingSpeed = EditorGUILayout.FloatField("Following speed: ", camera.followingSpeed); + } + + private void RotationTab() + { + using (new HorizontalBlock()) + { + GUILayout.Label("Keyboard input: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.useKeyboardRotation = EditorGUILayout.Toggle(camera.useKeyboardRotation); + } + if(camera.useKeyboardRotation) + { + camera.rotateLeftKey = (KeyCode)EditorGUILayout.EnumPopup("Rotate left: ", camera.rotateLeftKey); + camera.rotateRightKey = (KeyCode)EditorGUILayout.EnumPopup("Rotate right: ", camera.rotateRightKey); + camera.rotationSped = EditorGUILayout.FloatField("Keyboard rotation speed", camera.rotationSped); + } + + using (new HorizontalBlock()) + { + GUILayout.Label("Mouse input: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.useMouseRotation = EditorGUILayout.Toggle(camera.useMouseRotation); + } + if(camera.useMouseRotation) + { + camera.mouseRotationKey = (KeyCode)EditorGUILayout.EnumPopup("Mouse rotation key: ", camera.mouseRotationKey); + camera.mouseRotationSpeed = EditorGUILayout.FloatField("Mouse rotation speed: ", camera.mouseRotationSpeed); + } + } + + private void HeightTab() + { + using (new HorizontalBlock()) + { + GUILayout.Label("Auto height: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.autoHeight = EditorGUILayout.Toggle(camera.autoHeight); + } + if (camera.autoHeight) + { + camera.heightDampening = EditorGUILayout.FloatField("Height dampening: ", camera.heightDampening); + EditorGUILayout.PropertyField(serializedObject.FindProperty("groundMask")); + } + + using (new HorizontalBlock()) + { + GUILayout.Label("Keyboard zooming: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.useKeyboardZooming = EditorGUILayout.Toggle(camera.useKeyboardZooming); + } + if(camera.useKeyboardZooming) + { + camera.zoomInKey = (KeyCode)EditorGUILayout.EnumPopup("Zoom In: ", camera.zoomInKey); + camera.zoomOutKey = (KeyCode)EditorGUILayout.EnumPopup("Zoom Out: ", camera.zoomOutKey); + camera.keyboardZoomingSensitivity = EditorGUILayout.FloatField("Keyboard sensitivity: ", camera.keyboardZoomingSensitivity); + } + + using (new HorizontalBlock()) + { + GUILayout.Label("Scrollwheel zooming: ", EditorStyles.boldLabel, GUILayout.Width(170f)); + camera.useScrollwheelZooming = EditorGUILayout.Toggle(camera.useScrollwheelZooming); + } + if (camera.useScrollwheelZooming) + camera.scrollWheelZoomingSensitivity = EditorGUILayout.FloatField("Scrollwheel sensitivity: ", camera.scrollWheelZoomingSensitivity); + + if (camera.useScrollwheelZooming || camera.useKeyboardZooming) + { + using (new HorizontalBlock()) + { + camera.maxHeight = EditorGUILayout.FloatField("Max height: ", camera.maxHeight); + camera.minHeight = EditorGUILayout.FloatField("Min height: ", camera.minHeight); + } + } + } + } +}
\ No newline at end of file diff --git a/Assets/RTS_Camera/Scripts/Editor/RTS_CameraEditor.cs.meta b/Assets/RTS_Camera/Scripts/Editor/RTS_CameraEditor.cs.meta new file mode 100644 index 0000000..2c39be4 --- /dev/null +++ b/Assets/RTS_Camera/Scripts/Editor/RTS_CameraEditor.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: f43923eb579b5ab44bc30687c03d3abe +timeCreated: 1438769886 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RTS_Camera/Scripts/RTS_Camera.cs b/Assets/RTS_Camera/Scripts/RTS_Camera.cs new file mode 100644 index 0000000..5e5ee76 --- /dev/null +++ b/Assets/RTS_Camera/Scripts/RTS_Camera.cs @@ -0,0 +1,344 @@ +using UnityEngine; +using System.Collections; + +namespace RTS_Cam +{ + [RequireComponent(typeof(Camera))] + [AddComponentMenu("RTS Camera")] + public class RTS_Camera : MonoBehaviour + { + + #region Foldouts + +#if UNITY_EDITOR + + public int lastTab = 0; + + public bool movementSettingsFoldout; + public bool zoomingSettingsFoldout; + public bool rotationSettingsFoldout; + public bool heightSettingsFoldout; + public bool mapLimitSettingsFoldout; + public bool targetingSettingsFoldout; + public bool inputSettingsFoldout; + +#endif + + #endregion + + private Transform m_Transform; //camera tranform + public bool useFixedUpdate = false; //use FixedUpdate() or Update() + + #region Movement + + public float keyboardMovementSpeed = 5f; //speed with keyboard movement + public float screenEdgeMovementSpeed = 3f; //spee with screen edge movement + public float followingSpeed = 5f; //speed when following a target + public float rotationSped = 3f; + public float panningSpeed = 10f; + public float mouseRotationSpeed = 10f; + + #endregion + + #region Height + + public bool autoHeight = true; + public LayerMask groundMask = -1; //layermask of ground or other objects that affect height + + public float maxHeight = 10f; //maximal height + public float minHeight = 15f; //minimnal height + public float heightDampening = 5f; + public float keyboardZoomingSensitivity = 2f; + public float scrollWheelZoomingSensitivity = 25f; + + private float zoomPos = 0; //value in range (0, 1) used as t in Matf.Lerp + + #endregion + + #region MapLimits + + public bool limitMap = true; + public float limitX = 50f; //x limit of map + public float limitY = 50f; //z limit of map + + #endregion + + #region Targeting + + public Transform targetFollow; //target to follow + public Vector3 targetOffset; + + /// <summary> + /// are we following target + /// </summary> + public bool FollowingTarget + { + get + { + return targetFollow != null; + } + } + + #endregion + + #region Input + + public bool useScreenEdgeInput = true; + public float screenEdgeBorder = 25f; + + public bool useKeyboardInput = true; + public string horizontalAxis = "Horizontal"; + public string verticalAxis = "Vertical"; + + public bool usePanning = true; + public KeyCode panningKey = KeyCode.Mouse2; + + public bool useKeyboardZooming = true; + public KeyCode zoomInKey = KeyCode.E; + public KeyCode zoomOutKey = KeyCode.Q; + + public bool useScrollwheelZooming = true; + public string zoomingAxis = "Mouse ScrollWheel"; + + public bool useKeyboardRotation = true; + public KeyCode rotateRightKey = KeyCode.X; + public KeyCode rotateLeftKey = KeyCode.Z; + + public bool useMouseRotation = true; + public KeyCode mouseRotationKey = KeyCode.Mouse1; + + private Vector2 KeyboardInput + { + get { return useKeyboardInput ? new Vector2(Input.GetAxis(horizontalAxis), Input.GetAxis(verticalAxis)) : Vector2.zero; } + } + + private Vector2 MouseInput + { + get { return Input.mousePosition; } + } + + private float ScrollWheel + { + get { return Input.GetAxis(zoomingAxis); } + } + + private Vector2 MouseAxis + { + get { return new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); } + } + + private int ZoomDirection + { + get + { + bool zoomIn = Input.GetKey(zoomInKey); + bool zoomOut = Input.GetKey(zoomOutKey); + if (zoomIn && zoomOut) + return 0; + else if (!zoomIn && zoomOut) + return 1; + else if (zoomIn && !zoomOut) + return -1; + else + return 0; + } + } + + private int RotationDirection + { + get + { + bool rotateRight = Input.GetKey(rotateRightKey); + bool rotateLeft = Input.GetKey(rotateLeftKey); + if(rotateLeft && rotateRight) + return 0; + else if(rotateLeft && !rotateRight) + return -1; + else if(!rotateLeft && rotateRight) + return 1; + else + return 0; + } + } + + #endregion + + #region Unity_Methods + + private void Start() + { + m_Transform = transform; + } + + private void Update() + { + if (!useFixedUpdate) + CameraUpdate(); + } + + private void FixedUpdate() + { + if (useFixedUpdate) + CameraUpdate(); + } + + #endregion + + #region RTSCamera_Methods + + /// <summary> + /// update camera movement and rotation + /// </summary> + private void CameraUpdate() + { + if (FollowingTarget) + FollowTarget(); + else + Move(); + + HeightCalculation(); + Rotation(); + LimitPosition(); + } + + /// <summary> + /// move camera with keyboard or with screen edge + /// </summary> + private void Move() + { + if (useKeyboardInput) + { + Vector3 desiredMove = new Vector3(KeyboardInput.x, 0, KeyboardInput.y); + + desiredMove *= keyboardMovementSpeed; + desiredMove *= Time.deltaTime; + desiredMove = Quaternion.Euler(new Vector3(0f, transform.eulerAngles.y, 0f)) * desiredMove; + desiredMove = m_Transform.InverseTransformDirection(desiredMove); + + m_Transform.Translate(desiredMove, Space.Self); + } + + if (useScreenEdgeInput) + { + Vector3 desiredMove = new Vector3(); + + Rect leftRect = new Rect(0, 0, screenEdgeBorder, Screen.height); + Rect rightRect = new Rect(Screen.width - screenEdgeBorder, 0, screenEdgeBorder, Screen.height); + Rect upRect = new Rect(0, Screen.height - screenEdgeBorder, Screen.width, screenEdgeBorder); + Rect downRect = new Rect(0, 0, Screen.width, screenEdgeBorder); + + desiredMove.x = leftRect.Contains(MouseInput) ? -1 : rightRect.Contains(MouseInput) ? 1 : 0; + desiredMove.z = upRect.Contains(MouseInput) ? 1 : downRect.Contains(MouseInput) ? -1 : 0; + + desiredMove *= screenEdgeMovementSpeed; + desiredMove *= Time.deltaTime; + desiredMove = Quaternion.Euler(new Vector3(0f, transform.eulerAngles.y, 0f)) * desiredMove; + desiredMove = m_Transform.InverseTransformDirection(desiredMove); + + m_Transform.Translate(desiredMove, Space.Self); + } + + if(usePanning && Input.GetKey(panningKey) && MouseAxis != Vector2.zero) + { + Vector3 desiredMove = new Vector3(-MouseAxis.x, 0, -MouseAxis.y); + + desiredMove *= panningSpeed; + desiredMove *= Time.deltaTime; + desiredMove = Quaternion.Euler(new Vector3(0f, transform.eulerAngles.y, 0f)) * desiredMove; + desiredMove = m_Transform.InverseTransformDirection(desiredMove); + + m_Transform.Translate(desiredMove, Space.Self); + } + } + + /// <summary> + /// calcualte height + /// </summary> + private void HeightCalculation() + { + float distanceToGround = DistanceToGround(); + if(useScrollwheelZooming) + zoomPos += ScrollWheel * Time.deltaTime * scrollWheelZoomingSensitivity; + if (useKeyboardZooming) + zoomPos += ZoomDirection * Time.deltaTime * keyboardZoomingSensitivity; + + zoomPos = Mathf.Clamp01(zoomPos); + + float targetHeight = Mathf.Lerp(minHeight, maxHeight, zoomPos); + float difference = 0; + + if(distanceToGround != targetHeight) + difference = targetHeight - distanceToGround; + + m_Transform.position = Vector3.Lerp(m_Transform.position, + new Vector3(m_Transform.position.x, targetHeight + difference, m_Transform.position.z), Time.deltaTime * heightDampening); + } + + /// <summary> + /// rotate camera + /// </summary> + private void Rotation() + { + if(useKeyboardRotation) + transform.Rotate(Vector3.up, RotationDirection * Time.deltaTime * rotationSped, Space.World); + + if (useMouseRotation && Input.GetKey(mouseRotationKey)) + m_Transform.Rotate(Vector3.up, -MouseAxis.x * Time.deltaTime * mouseRotationSpeed, Space.World); + } + + /// <summary> + /// follow targetif target != null + /// </summary> + private void FollowTarget() + { + Vector3 targetPos = new Vector3(targetFollow.position.x, m_Transform.position.y, targetFollow.position.z) + targetOffset; + m_Transform.position = Vector3.MoveTowards(m_Transform.position, targetPos, Time.deltaTime * followingSpeed); + } + + /// <summary> + /// limit camera position + /// </summary> + private void LimitPosition() + { + if (!limitMap) + return; + + m_Transform.position = new Vector3(Mathf.Clamp(m_Transform.position.x, -limitX, limitX), + m_Transform.position.y, + Mathf.Clamp(m_Transform.position.z, -limitY, limitY)); + } + + /// <summary> + /// set the target + /// </summary> + /// <param name="target"></param> + public void SetTarget(Transform target) + { + targetFollow = target; + } + + /// <summary> + /// reset the target (target is set to null) + /// </summary> + public void ResetTarget() + { + targetFollow = null; + } + + /// <summary> + /// calculate distance to ground + /// </summary> + /// <returns></returns> + private float DistanceToGround() + { + Ray ray = new Ray(m_Transform.position, Vector3.down); + RaycastHit hit; + if (Physics.Raycast(ray, out hit, groundMask.value)) + return (hit.point - m_Transform.position).magnitude; + + return 0f; + } + + #endregion + } +}
\ No newline at end of file diff --git a/Assets/RTS_Camera/Scripts/RTS_Camera.cs.meta b/Assets/RTS_Camera/Scripts/RTS_Camera.cs.meta new file mode 100644 index 0000000..70a41e8 --- /dev/null +++ b/Assets/RTS_Camera/Scripts/RTS_Camera.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 28ef8c68bade09b41aca258d42a632f2 +timeCreated: 1438769867 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/TestSimulation.unity b/Assets/Scenes/TestSimulation.unity index 5e743b5..b2a968b 100644 --- a/Assets/Scenes/TestSimulation.unity +++ b/Assets/Scenes/TestSimulation.unity @@ -363,6 +363,7 @@ GameObject: - component: {fileID: 534669905} - component: {fileID: 534669904} - component: {fileID: 534669903} + - component: {fileID: 534669906} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -388,7 +389,7 @@ Camera: m_Enabled: 1 serializedVersion: 2 m_ClearFlags: 1 - m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_BackGroundColor: {r: 0.19215688, g: 0.46801388, b: 0.4745098, a: 0} m_projectionMatrixMode: 1 m_SensorSize: {x: 36, y: 24} m_LensShift: {x: 0, y: 0} @@ -402,7 +403,7 @@ Camera: height: 1 near clip plane: 0.3 far clip plane: 1000 - field of view: 60 + field of view: 70 orthographic: 0 orthographic size: 5 m_Depth: -1 @@ -427,13 +428,71 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 534669902} - m_LocalRotation: {x: 0.22420184, y: -0, z: -0, w: 0.9745428} - m_LocalPosition: {x: 0, y: 5.978, z: -9.774} + m_LocalRotation: {x: 0.5, y: 0, z: 0, w: 0.8660254} + m_LocalPosition: {x: 0, y: 20, z: -5} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 0 - m_LocalEulerAnglesHint: {x: 25.912, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 60, y: 0, z: 0} +--- !u!114 &534669906 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 534669902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 28ef8c68bade09b41aca258d42a632f2, type: 3} + m_Name: + m_EditorClassIdentifier: + lastTab: 1 + movementSettingsFoldout: 0 + zoomingSettingsFoldout: 0 + rotationSettingsFoldout: 0 + heightSettingsFoldout: 0 + mapLimitSettingsFoldout: 0 + targetingSettingsFoldout: 0 + inputSettingsFoldout: 0 + useFixedUpdate: 0 + keyboardMovementSpeed: 20 + screenEdgeMovementSpeed: 3 + followingSpeed: 5 + rotationSped: 3 + panningSpeed: 10 + mouseRotationSpeed: 20 + autoHeight: 1 + groundMask: + serializedVersion: 2 + m_Bits: 4294967295 + maxHeight: 10 + minHeight: 15 + heightDampening: 5 + keyboardZoomingSensitivity: 2 + scrollWheelZoomingSensitivity: 50 + limitMap: 1 + limitX: 20 + limitY: 20 + targetFollow: {fileID: 0} + targetOffset: {x: 0, y: 0, z: 0} + useScreenEdgeInput: 0 + screenEdgeBorder: 25 + useKeyboardInput: 1 + horizontalAxis: Horizontal + verticalAxis: Vertical + usePanning: 1 + panningKey: 325 + useKeyboardZooming: 1 + zoomInKey: 91 + zoomOutKey: 93 + useScrollwheelZooming: 1 + zoomingAxis: Mouse ScrollWheel + useKeyboardRotation: 1 + rotateRightKey: 120 + rotateLeftKey: 122 + useMouseRotation: 1 + mouseRotationKey: 324 --- !u!1 &1353580675 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Player/PlayerMovement.cs b/Assets/Scripts/Player/PlayerMovement.cs index f82a325..aa6dd13 100644 --- a/Assets/Scripts/Player/PlayerMovement.cs +++ b/Assets/Scripts/Player/PlayerMovement.cs @@ -17,8 +17,6 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. * *********************************************************************************/ -using System.Collections; -using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; |
