| | 1 | | using AvatarSystem; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class AvatarMovementController : MonoBehaviour, IPoolLifecycleHandler, IAvatarMovementController |
| | 9 | | { |
| | 10 | | private const float SPEED_SLOW = 2.0f; |
| | 11 | | private const float SPEED_FAST = 4.0f; |
| | 12 | | private const float SPEED_ULTRA_FAST = 8.0f; |
| | 13 | | private const float SPEED_GRAVITY = 8.0f; |
| | 14 | | private const float ROTATION_SPEED = 6.25f; |
| | 15 | | private const float SPEED_EPSILON = 0.0001f; |
| 339 | 16 | | private float movementSpeed = SPEED_SLOW; |
| | 17 | |
|
| | 18 | | private Transform avatarTransformValue; |
| | 19 | |
|
| | 20 | | private Quaternion targetRotation; |
| 339 | 21 | | private Vector3 currentWorldPosition = Vector3.zero; |
| | 22 | | private Vector3 targetPosition; |
| | 23 | |
|
| | 24 | | private float movementLerpWait = 0f; |
| | 25 | | private float movementLerpWaitCounter = 0f; |
| | 26 | |
|
| | 27 | |
|
| | 28 | | private Transform AvatarTransform |
| | 29 | | { |
| | 30 | | get |
| | 31 | | { |
| 2 | 32 | | if (avatarTransformValue == null) |
| | 33 | | { |
| 1 | 34 | | avatarTransformValue = GetComponent<AvatarShape>().entity.gameObject.transform; |
| 1 | 35 | | enabled = true; |
| | 36 | | } |
| | 37 | |
|
| 2 | 38 | | return avatarTransformValue; |
| | 39 | | } |
| | 40 | | set |
| | 41 | | { |
| 13 | 42 | | avatarTransformValue = value; |
| | 43 | |
|
| 13 | 44 | | if (value == null) |
| 5 | 45 | | enabled = false; |
| 13 | 46 | | } |
| | 47 | | } |
| | 48 | |
|
| | 49 | | private Vector3 CurrentPosition |
| | 50 | | { |
| 1 | 51 | | get { return currentWorldPosition; } |
| | 52 | | set |
| | 53 | | { |
| 0 | 54 | | currentWorldPosition = value; |
| 0 | 55 | | Vector3 newPosition = PositionUtils.WorldToUnityPosition(currentWorldPosition); |
| 0 | 56 | | if (float.IsNaN(newPosition.x) || float.IsInfinity(newPosition.x) || |
| | 57 | | float.IsNaN(newPosition.z) || float.IsInfinity(newPosition.z) || |
| | 58 | | float.IsNaN(newPosition.y) || float.IsInfinity(newPosition.y)) |
| 0 | 59 | | return; |
| | 60 | |
|
| 0 | 61 | | AvatarTransform.position = newPosition; |
| 0 | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | private Quaternion CurrentRotation |
| | 66 | | { |
| 1 | 67 | | get { return AvatarTransform.rotation; } |
| 2 | 68 | | set { AvatarTransform.rotation = value; } |
| | 69 | | } |
| | 70 | |
|
| 5 | 71 | | public void OnPoolGet() { } |
| | 72 | |
|
| | 73 | | public void OnPoolRelease() |
| | 74 | | { |
| 5 | 75 | | AvatarTransform = null; |
| | 76 | |
|
| 5 | 77 | | currentWorldPosition = Vector3.zero; |
| 5 | 78 | | } |
| | 79 | |
|
| | 80 | | public void SetAvatarTransform(Transform avatarTransform) |
| | 81 | | { |
| 8 | 82 | | AvatarTransform = avatarTransform; |
| 8 | 83 | | } |
| | 84 | |
|
| 676 | 85 | | private void OnEnable() { CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; } |
| | 86 | |
|
| 676 | 87 | | private void OnDisable() { CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; } |
| | 88 | |
|
| | 89 | | private void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 90 | | { |
| 0 | 91 | | AvatarTransform.position = PositionUtils.WorldToUnityPosition(currentWorldPosition); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | public void OnTransformChanged(object model) |
| | 95 | | { |
| 0 | 96 | | DCLTransform.Model transformModel = (DCLTransform.Model)model; |
| 0 | 97 | | OnTransformChanged(transformModel.position, transformModel.rotation, false); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public void OnTransformChanged(in Vector3 position, in Quaternion rotation, bool inmediate) |
| | 101 | | { |
| 0 | 102 | | var offsetPosition = new Vector3(0, DCLCharacterController.i.characterController.height * 0.5f, 0); |
| 0 | 103 | | MoveTo( |
| | 104 | | position - offsetPosition, // To fix the "always flying" avatars issue, We report the chara's centered p |
| | 105 | | rotation, |
| | 106 | | inmediate); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public void MoveTo(Vector3 position, Quaternion rotation, bool immediate = false) |
| | 110 | | { |
| 0 | 111 | | if (immediate) |
| | 112 | | { |
| 0 | 113 | | CurrentPosition = position; |
| 0 | 114 | | AvatarTransform.rotation = rotation; |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | Vector3 flatEulerRotation = rotation.eulerAngles; |
| 0 | 118 | | flatEulerRotation.z = flatEulerRotation.x = 0; |
| 0 | 119 | | rotation = Quaternion.Euler(flatEulerRotation); |
| | 120 | |
|
| 0 | 121 | | targetPosition = position; |
| 0 | 122 | | targetRotation = rotation; |
| | 123 | |
|
| 0 | 124 | | float distance = Vector3.Distance(targetPosition, currentWorldPosition); |
| | 125 | |
|
| 0 | 126 | | if (distance >= 50) |
| 0 | 127 | | movementSpeed = float.MaxValue; |
| 0 | 128 | | else if (distance >= 3) |
| 0 | 129 | | movementSpeed = Mathf.Lerp(SPEED_SLOW, SPEED_ULTRA_FAST, (distance - 3) / 10.0f); |
| | 130 | | else |
| 0 | 131 | | movementSpeed = SPEED_SLOW; |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | void UpdateLerp(float deltaTime) |
| | 135 | | { |
| 1 | 136 | | if (Vector3.SqrMagnitude(CurrentPosition - targetPosition) < SPEED_EPSILON) |
| | 137 | | { |
| 1 | 138 | | UpdateRotation(deltaTime, targetRotation); |
| 1 | 139 | | return; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | //NOTE(Brian): When we update movement we don't update rotation, because the Avatar will face the movement f |
| 0 | 143 | | UpdateMovement(deltaTime); |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | private void UpdateRotation(float deltaTime, Quaternion targetRotation) |
| | 147 | | { |
| 1 | 148 | | CurrentRotation = Quaternion.Slerp(CurrentRotation, targetRotation, ROTATION_SPEED * deltaTime); |
| 1 | 149 | | } |
| | 150 | |
|
| | 151 | | private void UpdateMovement(float deltaTime) |
| | 152 | | { |
| 0 | 153 | | Vector3 flattenedDiff = targetPosition - CurrentPosition; |
| 0 | 154 | | flattenedDiff.y = 0; |
| | 155 | |
|
| | 156 | | //NOTE(Brian): Avoid Unity error when computing look rotation for 0 magnitude vectors. |
| | 157 | | // Note that this isn't the same as the previous distance check because this |
| | 158 | | // is computed with a flattened vector. |
| 0 | 159 | | if (flattenedDiff != Vector3.zero) |
| | 160 | | { |
| 0 | 161 | | Quaternion lookRotation = Quaternion.LookRotation(flattenedDiff, Vector3.up); |
| 0 | 162 | | UpdateRotation(deltaTime, lookRotation); |
| | 163 | | } |
| | 164 | |
|
| 0 | 165 | | Vector3 direction = (targetPosition - CurrentPosition).normalized; |
| 0 | 166 | | Vector3 delta = direction * (movementSpeed * deltaTime); |
| | 167 | |
|
| | 168 | | //NOTE(Brian): We need a separate value for Y movement because the gravity has to be lerped faster. |
| 0 | 169 | | delta.y = direction.y * SPEED_GRAVITY * deltaTime; |
| | 170 | |
|
| | 171 | | //NOTE(Brian): If we overshoot targetPosition we adjust the delta value accordingly. |
| 0 | 172 | | if (delta.sqrMagnitude > Vector3.SqrMagnitude(targetPosition - CurrentPosition)) |
| | 173 | | { |
| 0 | 174 | | delta = targetPosition - CurrentPosition; |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | CurrentPosition += delta; |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | private void Update() |
| | 181 | | { |
| 1 | 182 | | movementLerpWaitCounter += Time.deltaTime; |
| 1 | 183 | | if (movementLerpWaitCounter >= movementLerpWait) |
| | 184 | | { |
| 1 | 185 | | UpdateLerp(movementLerpWaitCounter); |
| 1 | 186 | | movementLerpWaitCounter = 0f; |
| | 187 | | } |
| 1 | 188 | | } |
| | 189 | |
|
| 0 | 190 | | public void SetMovementLerpWait(float secondsBetweenUpdates) { movementLerpWait = secondsBetweenUpdates; } |
| | 191 | | } |
| | 192 | | } |