From 8a9cc2843564d853af73b8f9af224e0a7e34a99b Mon Sep 17 00:00:00 2001 From: Nickk888 <35627647+Nickk888SAMP@users.noreply.github.com> Date: Tue, 11 Jul 2023 00:46:52 +0200 Subject: [PATCH] Fixed controller be dependent on Time Scale --- Prefabs/RTSCameraController.prefab | 1 + Scenes/ExampleScene.unity | 12 +++++ Scripts/RTSCameraTargetController.cs | 65 ++++++++++++++-------------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/Prefabs/RTSCameraController.prefab b/Prefabs/RTSCameraController.prefab index 312d906b..1c0cf2ce 100644 --- a/Prefabs/RTSCameraController.prefab +++ b/Prefabs/RTSCameraController.prefab @@ -1105,6 +1105,7 @@ MonoBehaviour: serializedVersion: 2 m_Bits: 8 cameraTarget: {fileID: 5524602137987408595} + independentTimeScale: 1 allowRotate: 1 allowTiltRotate: 1 allowZoom: 1 diff --git a/Scenes/ExampleScene.unity b/Scenes/ExampleScene.unity index cd522ba8..6a22fc40 100644 --- a/Scenes/ExampleScene.unity +++ b/Scenes/ExampleScene.unity @@ -4032,6 +4032,14 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 1835259192517180277, guid: 48b147c9aa2a8394082c7e10a44f6b94, type: 3} + propertyPath: m_Enabled + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 5524602137149729665, guid: 48b147c9aa2a8394082c7e10a44f6b94, type: 3} + propertyPath: m_IsActive + value: 1 + objectReference: {fileID: 0} - target: {fileID: 5524602137660462666, guid: 48b147c9aa2a8394082c7e10a44f6b94, type: 3} propertyPath: m_AnchorMax.x value: 0 @@ -4092,6 +4100,10 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 5524602138545185833, guid: 48b147c9aa2a8394082c7e10a44f6b94, type: 3} + propertyPath: m_IsActive + value: 1 + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 48b147c9aa2a8394082c7e10a44f6b94, type: 3} --- !u!1 &615080560 diff --git a/Scripts/RTSCameraTargetController.cs b/Scripts/RTSCameraTargetController.cs index db75612e..cdc18873 100644 --- a/Scripts/RTSCameraTargetController.cs +++ b/Scripts/RTSCameraTargetController.cs @@ -4,39 +4,40 @@ public class RTSCameraTargetController : MonoBehaviour { - [SerializeField] + [SerializeField] [Tooltip("The Cinemachine Virtual Camera to be controlled by the controller.")] private CinemachineVirtualCamera virtualCamera; - [SerializeField] + [SerializeField] [Tooltip("The ground layer for the height check.")] private LayerMask groundLayer; - [SerializeField] + [SerializeField] [Tooltip("The target for the camera to follow.")] private Transform cameraTarget; - [Space] - [Header("Properties")] + [Space] [Header("Time Scale")] + [SerializeField] [Tooltip("Check to make the controller be independent on the Time Scale.")] + private bool independentTimeScale = true; + + [Space][Header("Properties")] [SerializeField][Tooltip("Allows or Disallows rotation of the Camera.")] private bool allowRotate = true; - [SerializeField][Tooltip("Allows or Disallows rotation of the Cameras Tilt.")] + [SerializeField] [Tooltip("Allows or Disallows rotation of the Cameras Tilt.")] private bool allowTiltRotate = true; - [SerializeField][Tooltip("Allows or Disallows Zooming.")] + [SerializeField] [Tooltip("Allows or Disallows Zooming.")] private bool allowZoom = true; - [SerializeField][Tooltip("Allows or Disallows mouse drag movement.")] + [SerializeField] [Tooltip("Allows or Disallows mouse drag movement.")] private bool allowDragMove = true; - [SerializeField][Tooltip("Allows or Disallows camera movement with keys/gamepad input.")] + [SerializeField] [Tooltip("Allows or Disallows camera movement with keys/gamepad input.")] private bool allowKeysMove = true; - [SerializeField][Tooltip("Allows or Disallows camera movement using the screen sides.")] + [SerializeField] [Tooltip("Allows or Disallows camera movement using the screen sides.")] private bool allowScreenSideMove = true; - - [Space] - [Header("Camera")] - [SerializeField][Tooltip("The Minimum and Maximum Tilt of the camera, valid range: 0-89 (Do not go 90 or above)")] + [Space] [Header("Camera")] + [SerializeField] [Tooltip("The Minimum and Maximum Tilt of the camera, valid range: 0-89 (Do not go 90 or above)")] private Vector2 cameraTiltMinMax = new Vector2(15.0f, 75.0f); [SerializeField] @@ -68,8 +69,7 @@ [Space] [Header("Camera Zoom")] [SerializeField] private float cameraZoomSmoothTime = 7f; - [SerializeField] - [Tooltip("The Minimum and Maximum zoom factor. X = Min | Y = Max")] + [SerializeField] [Tooltip("The Minimum and Maximum zoom factor. X = Min | Y = Max")] private Vector2 cameraZoomMinMax = new Vector2(5, 100); [Space] @@ -131,7 +131,7 @@ private void Start() rotateCameraCanvasGameObject.SetActive(false); } - void Update() + private void Update() { Vector3 vectorChange = Vector3.zero; Vector3 mousePos = Input.mousePosition; @@ -156,7 +156,7 @@ private void GroundHeightCorrection() { if (Physics.Raycast(new Vector3(cameraTarget.position.x, 9999999, cameraTarget.position.z), Vector3.down, out RaycastHit hit, Mathf.Infinity, groundLayer)) { - cameraTarget.position = Vector3.Lerp(cameraTarget.position, new Vector3(cameraTarget.position.x, hit.point.y, cameraTarget.position.z), cameraTargetGroundHeightCheckSmoothTime * Time.deltaTime); + cameraTarget.position = Vector3.Lerp(cameraTarget.position, new Vector3(cameraTarget.position.x, hit.point.y, cameraTarget.position.z), cameraTargetGroundHeightCheckSmoothTime * GetTimeScale()); } } } @@ -173,7 +173,7 @@ private void HandleTargetLock() } else { - cameraTarget.position = Vector3.Lerp(cameraTarget.position, lockedOnPosition, targetLockSpeed * Time.deltaTime); + cameraTarget.position = Vector3.Lerp(cameraTarget.position, lockedOnPosition, targetLockSpeed * GetTimeScale()); } } else @@ -184,10 +184,10 @@ private void HandleTargetLock() } else { - cameraTarget.position = Vector3.Lerp(cameraTarget.position, lockedOnTransform.position, targetLockSpeed * Time.deltaTime); + cameraTarget.position = Vector3.Lerp(cameraTarget.position, lockedOnTransform.position, targetLockSpeed * GetTimeScale()); } } - currentCameraZoom = Mathf.Lerp(currentCameraZoom, lockedOnZoom, targetLockSpeed * Time.deltaTime); + currentCameraZoom = Mathf.Lerp(currentCameraZoom, lockedOnZoom, targetLockSpeed * GetTimeScale()); } } @@ -211,8 +211,8 @@ private void HandleKeysMove() { if (!isDragging && !isSideZoneMoving && allowKeysMove) { - float horizontalInput = Input.GetAxis("Horizontal"); - float verticalInput = Input.GetAxis("Vertical"); + float horizontalInput = Input.GetAxisRaw("Horizontal"); + float verticalInput = Input.GetAxisRaw("Vertical"); Vector3 vectorChange = new Vector3(horizontalInput, 0, verticalInput); // Move target relative to Camera @@ -284,7 +284,7 @@ private void HandleZoom() CancelTargetLock(); } } - framingTransposer.m_CameraDistance = Mathf.SmoothDamp(framingTransposer.m_CameraDistance, currentCameraZoom, ref cameraZoomSmoothDampVel_ref, (cameraZoomSmoothTime / 100)); + framingTransposer.m_CameraDistance = Mathf.SmoothDamp(framingTransposer.m_CameraDistance, currentCameraZoom, ref cameraZoomSmoothDampVel_ref, (cameraZoomSmoothTime / 100), Mathf.Infinity, GetTimeScale()); if (cameraZoomSlider != null) { cameraZoomSlider.minValue = cameraZoomMinMax.x; @@ -315,8 +315,8 @@ private void HandleRotation() } if (Input.GetMouseButton(1) && allowRotate) { - float horizontalMouse = Input.GetAxis("Mouse X"); - float verticalMouse = Input.GetAxis("Mouse Y"); + float horizontalMouse = Input.GetAxisRaw("Mouse X"); + float verticalMouse = Input.GetAxisRaw("Mouse Y"); if (horizontalMouse != 0) { currentRotateDir = (horizontalMouse > 0 ? true : false); @@ -327,15 +327,12 @@ private void HandleRotation() } if (verticalMouse != 0 && allowTiltRotate) { - currentRotateDir = (verticalMouse > 0 ? true : false); currentCameraTilt += verticalMouse * cameraRotateSpeed; currentCameraTilt = Mathf.Clamp(currentCameraTilt, cameraTiltMinMax.x, cameraTiltMinMax.y); virtualCameraGameObject.transform.eulerAngles = new Vector3(currentCameraTilt, virtualCameraGameObject.transform.eulerAngles.y, 0); - if (rotateCameraCanvasGameObject != null) - rotateCameraCanvasGameObject.transform.rotation = Quaternion.Euler(rotateCameraCanvasGameObject.transform.eulerAngles.x, rotateCameraCanvasGameObject.transform.eulerAngles.y, virtualCameraGameObject.transform.eulerAngles.y * (currentRotateDir ? 1 : -1)); } if (rotateCameraCanvasGameObject != null) - rotateCameraCanvasGameObject.transform.rotation = Quaternion.Euler(rotateCameraCanvasGameObject.transform.eulerAngles.x, Mathf.SmoothDamp(rotateCameraCanvasGameObject.transform.eulerAngles.y, (currentRotateDir ? 180 : 0), ref RotateFlipSmoothDampVel_ref, 0.1f), rotateCameraCanvasGameObject.transform.eulerAngles.z); + rotateCameraCanvasGameObject.transform.rotation = Quaternion.Euler(rotateCameraCanvasGameObject.transform.eulerAngles.x, Mathf.SmoothDamp(rotateCameraCanvasGameObject.transform.eulerAngles.y, (currentRotateDir ? 180 : 0), ref RotateFlipSmoothDampVel_ref, 0.1f, Mathf.Infinity, GetTimeScale()), rotateCameraCanvasGameObject.transform.eulerAngles.z); } } } @@ -351,10 +348,14 @@ private void MoveTargetRelativeToCamera(Vector3 direction, float speed) camRight.Normalize(); Vector3 relativeDir = (camForward * direction.z) + (camRight * direction.x); - cameraTarget.Translate(relativeDir * (relativeZoomCameraMoveSpeed * speed) * Time.deltaTime); + cameraTarget.Translate(relativeDir * (relativeZoomCameraMoveSpeed * speed) * GetTimeScale()); + } + + private float GetTimeScale() + { + return (independentTimeScale ? Time.unscaledDeltaTime : Time.deltaTime); } - private void GetMouseScreenSide(Vector3 mousePosition, out int width, out int height) { int heightPos = 0;