| | 1 | | using DCL; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCLFeatures.ScreencaptureCamera.CameraObject |
| | 5 | | { |
| | 6 | | public class ScreencaptureCameraTranslation |
| | 7 | | { |
| | 8 | | private readonly TranslationInputSchema input; |
| | 9 | |
|
| 0 | 10 | | private Vector3 moveVector = Vector3.zero; |
| | 11 | |
|
| 0 | 12 | | public ScreencaptureCameraTranslation(TranslationInputSchema input) |
| | 13 | | { |
| 0 | 14 | | this.input = input; |
| 0 | 15 | | } |
| | 16 | |
|
| | 17 | | public void Translate(CharacterController target, float moveSpeed, float maxDistanceFromPlayer, float deltaTime) |
| | 18 | | { |
| 0 | 19 | | moveVector = GetMoveVectorFromInput(target.transform, moveSpeed, deltaTime); |
| 0 | 20 | | target.Move(RestrictedMovementBySemiSphere(target.transform, moveVector, maxDistanceFromPlayer)); |
| 0 | 21 | | } |
| | 22 | |
|
| | 23 | | private Vector3 GetMoveVectorFromInput(Transform target, float moveSpeed, float deltaTime) |
| | 24 | | { |
| 0 | 25 | | Vector3 forward = target.forward.normalized * input.YAxis.GetValue(); |
| 0 | 26 | | Vector3 horizontal = target.right.normalized * input.XAxis.GetValue(); |
| | 27 | |
|
| 0 | 28 | | float verticalDirection = input.UpAction.isOn ? 1 : |
| | 29 | | input.DownAction.isOn ? -1 : 0f; |
| | 30 | |
|
| 0 | 31 | | Vector3 vertical = target.up.normalized * verticalDirection; |
| | 32 | |
|
| 0 | 33 | | return (forward + horizontal + vertical) * (moveSpeed * deltaTime); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | private static Vector3 RestrictedMovementBySemiSphere(Transform target, Vector3 movementVector, float maxDistanc |
| | 37 | | { |
| 0 | 38 | | if (target.position.y + movementVector.y <= 0f) |
| 0 | 39 | | movementVector.y = 0f; |
| | 40 | |
|
| 0 | 41 | | Vector3 playerPosition = DataStore.i.player.playerUnityPosition.Get(); |
| 0 | 42 | | Vector3 desiredCameraPosition = target.position + movementVector; |
| | 43 | |
|
| 0 | 44 | | float distanceFromPlayer = Vector3.Distance(desiredCameraPosition, playerPosition); |
| | 45 | |
|
| | 46 | | // If the distance is greater than the allowed radius, correct the movement vector |
| 0 | 47 | | if (distanceFromPlayer > maxDistanceFromPlayer) |
| | 48 | | { |
| 0 | 49 | | Vector3 directionFromPlayer = (desiredCameraPosition - playerPosition).normalized; |
| 0 | 50 | | desiredCameraPosition = playerPosition + (directionFromPlayer * maxDistanceFromPlayer); |
| 0 | 51 | | movementVector = desiredCameraPosition - target.position; |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | return movementVector; |
| | 55 | | } |
| | 56 | | } |
| | 57 | | } |