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