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