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