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