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