| | 1 | | using Cinemachine; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Camera |
| | 5 | | { |
| | 6 | | public class CameraDampOnGroundType |
| | 7 | | { |
| | 8 | | private const float AFTER_TELEPORT_DAMP_TIME_MS = 50f; |
| | 9 | |
|
| | 10 | | [System.Serializable] |
| | 11 | | public class Settings |
| | 12 | | { |
| 0 | 13 | | public bool enabled = true; |
| | 14 | | public float dampingOnAir; |
| | 15 | | public float dampingOnGround; |
| | 16 | | public float dampingOnMovingPlatform; |
| | 17 | |
|
| | 18 | | [Tooltip("Min raycast ground distance to be considered airborne")] |
| | 19 | | public float groundCheckThreshold; |
| | 20 | | } |
| | 21 | |
|
| | 22 | | private FollowWithDamping followWithDamping; |
| | 23 | | public Settings settings; |
| | 24 | | private float zeroDampTime; |
| | 25 | |
|
| 597 | 26 | | public CameraDampOnGroundType (Settings settings, FollowWithDamping followWithDamping) |
| | 27 | | { |
| 597 | 28 | | this.settings = settings; |
| 597 | 29 | | this.followWithDamping = followWithDamping; |
| 597 | 30 | | DataStore.i.player.lastTeleportPosition.OnChange += OnTeleport; |
| 597 | 31 | | } |
| 78 | 32 | | private void OnTeleport(Vector3 current, Vector3 previous) { zeroDampTime = AFTER_TELEPORT_DAMP_TIME_MS; } |
| | 33 | |
|
| | 34 | | public void Update(bool hitGround, RaycastHit hitInfo) |
| | 35 | | { |
| 2198 | 36 | | if ( !settings.enabled ) |
| 2198 | 37 | | return; |
| | 38 | |
|
| 0 | 39 | | UpdateDamp(hitGround, hitInfo); |
| 0 | 40 | | UpdateAfterTeleportDamp(); |
| 0 | 41 | | } |
| | 42 | | private void UpdateDamp(bool hitGround, RaycastHit hitInfo) |
| | 43 | | { |
| 0 | 44 | | bool isOnMovingPlatform = DCLCharacterController.i.isOnMovingPlatform; |
| | 45 | |
|
| 0 | 46 | | if ( hitGround ) |
| | 47 | | { |
| 0 | 48 | | if ( hitInfo.distance < settings.groundCheckThreshold ) |
| 0 | 49 | | followWithDamping.damping.y = isOnMovingPlatform ? settings.dampingOnMovingPlatform : settings.dampi |
| | 50 | | else |
| 0 | 51 | | followWithDamping.damping.y = settings.dampingOnAir; |
| | 52 | | } |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | // This avoids the "Camera clips through stuff" after teleporting to different heights |
| | 56 | | private void UpdateAfterTeleportDamp() |
| | 57 | | { |
| 0 | 58 | | if (zeroDampTime > 0) |
| | 59 | | { |
| 0 | 60 | | followWithDamping.damping.y = 0; |
| 0 | 61 | | zeroDampTime -= Time.unscaledDeltaTime; |
| | 62 | | } |
| 0 | 63 | | } |
| | 64 | | } |
| | 65 | | } |