| | 1 | | using Cinemachine; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Interface; |
| | 6 | | using System; |
| | 7 | | using UnityEngine; |
| | 8 | | using Environment = DCL.Environment; |
| | 9 | |
|
| | 10 | | public class DCLCharacterController : MonoBehaviour |
| | 11 | | { |
| 8300 | 12 | | public static DCLCharacterController i { get; private set; } |
| | 13 | |
|
| | 14 | | private const float CONTROLLER_DRIFT_OFFSET = 0.15f; |
| | 15 | |
|
| | 16 | | [Header("Movement")] |
| 341 | 17 | | public float minimumYPosition = 1f; |
| | 18 | |
|
| 341 | 19 | | public float groundCheckExtraDistance = 0.1f; |
| 341 | 20 | | public float gravity = -55f; |
| 341 | 21 | | public float jumpForce = 12f; |
| 341 | 22 | | public float movementSpeed = 8f; |
| 341 | 23 | | public float runningSpeedMultiplier = 2f; |
| | 24 | |
|
| | 25 | | public DCLCharacterPosition characterPosition; |
| | 26 | |
|
| | 27 | | [Header("Collisions")] |
| | 28 | | public LayerMask groundLayers; |
| | 29 | |
|
| | 30 | | [Header("Additional Camera Layers")] |
| | 31 | | public LayerMask cameraLayers; |
| | 32 | |
|
| | 33 | | [NonSerialized] |
| | 34 | | public bool initialPositionAlreadySet = false; |
| | 35 | |
|
| | 36 | | [NonSerialized] |
| 341 | 37 | | public bool characterAlwaysEnabled = true; |
| | 38 | |
|
| | 39 | | [NonSerialized] |
| | 40 | | public CharacterController characterController; |
| | 41 | |
|
| | 42 | | FreeMovementController freeMovementController; |
| | 43 | |
|
| | 44 | | new Collider collider; |
| | 45 | |
|
| | 46 | | float lastUngroundedTime = 0f; |
| | 47 | | float lastJumpButtonPressedTime = 0f; |
| | 48 | | float lastMovementReportTime; |
| | 49 | | float originalGravity; |
| | 50 | | Vector3 lastLocalGroundPosition; |
| | 51 | |
|
| | 52 | | Vector3 lastCharacterRotation; |
| | 53 | | Vector3 lastGlobalCharacterRotation; |
| | 54 | |
|
| 341 | 55 | | Vector3 velocity = Vector3.zero; |
| | 56 | |
|
| 34 | 57 | | public bool isWalking { get; private set; } = false; |
| 34 | 58 | | public bool isMovingByUserInput { get; private set; } = false; |
| 6401 | 59 | | public bool isJumping { get; private set; } = false; |
| 29584 | 60 | | public bool isGrounded { get; private set; } |
| 35066 | 61 | | public bool isOnMovingPlatform { get; private set; } |
| | 62 | |
|
| | 63 | | internal Transform groundTransform; |
| | 64 | |
|
| | 65 | | Vector3 lastPosition; |
| | 66 | | Vector3 groundLastPosition; |
| | 67 | | Quaternion groundLastRotation; |
| | 68 | | bool jumpButtonPressed = false; |
| | 69 | |
|
| | 70 | | [Header("InputActions")] |
| | 71 | | public InputAction_Hold jumpAction; |
| | 72 | |
|
| | 73 | | public InputAction_Hold sprintAction; |
| | 74 | |
|
| | 75 | | public Vector3 moveVelocity; |
| | 76 | |
|
| | 77 | | private InputAction_Hold.Started jumpStartedDelegate; |
| | 78 | | private InputAction_Hold.Finished jumpFinishedDelegate; |
| | 79 | | private InputAction_Hold.Started walkStartedDelegate; |
| | 80 | | private InputAction_Hold.Finished walkFinishedDelegate; |
| | 81 | |
|
| 11552 | 82 | | private Vector3NullableVariable characterForward => CommonScriptableObjects.characterForward; |
| | 83 | |
|
| | 84 | | public static Action<DCLCharacterPosition> OnCharacterMoved; |
| | 85 | | public static Action<DCLCharacterPosition> OnPositionSet; |
| | 86 | | public event Action<float> OnUpdateFinish; |
| | 87 | |
|
| | 88 | | public GameObject avatarGameObject; |
| | 89 | | public GameObject firstPersonCameraGameObject; |
| | 90 | |
|
| | 91 | | [SerializeField] |
| | 92 | | private InputAction_Measurable characterYAxis; |
| | 93 | |
|
| | 94 | | [SerializeField] |
| | 95 | | private InputAction_Measurable characterXAxis; |
| | 96 | |
|
| 10923 | 97 | | private Vector3Variable cameraForward => CommonScriptableObjects.cameraForward; |
| 34 | 98 | | private Vector3Variable cameraRight => CommonScriptableObjects.cameraRight; |
| | 99 | |
|
| 341 | 100 | | private readonly DataStore_Player dataStorePlayer = DataStore.i.player; |
| | 101 | |
|
| | 102 | | [NonSerialized] |
| | 103 | | public float movingPlatformSpeed; |
| | 104 | | private CollisionFlags lastCharacterControllerCollision; |
| | 105 | |
|
| | 106 | | public event Action OnJump; |
| | 107 | | public event Action OnHitGround; |
| | 108 | | public event Action<float> OnMoved; |
| | 109 | |
|
| | 110 | | public void SetMovementInputToZero() |
| | 111 | | { |
| 0 | 112 | | characterXAxis.SetValue(0); |
| 0 | 113 | | characterYAxis.SetValue(0); |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | void Awake() |
| | 117 | | { |
| 324 | 118 | | if (i != null) |
| | 119 | | { |
| 22 | 120 | | Destroy(gameObject); |
| 22 | 121 | | return; |
| | 122 | | } |
| | 123 | |
|
| 302 | 124 | | i = this; |
| 302 | 125 | | originalGravity = gravity; |
| | 126 | |
|
| 302 | 127 | | SubscribeToInput(); |
| 302 | 128 | | CommonScriptableObjects.playerUnityPosition.Set(Vector3.zero); |
| 302 | 129 | | dataStorePlayer.playerWorldPosition.Set(Vector3.zero); |
| 302 | 130 | | CommonScriptableObjects.playerCoords.Set(Vector2Int.zero); |
| 302 | 131 | | dataStorePlayer.playerGridPosition.Set(Vector2Int.zero); |
| 302 | 132 | | CommonScriptableObjects.playerUnityEulerAngles.Set(Vector3.zero); |
| | 133 | |
|
| 302 | 134 | | characterPosition = new DCLCharacterPosition(); |
| 302 | 135 | | characterController = GetComponent<CharacterController>(); |
| 302 | 136 | | freeMovementController = GetComponent<FreeMovementController>(); |
| 302 | 137 | | collider = GetComponent<Collider>(); |
| | 138 | |
|
| 302 | 139 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; |
| | 140 | |
|
| 302 | 141 | | lastPosition = transform.position; |
| 302 | 142 | | transform.parent = null; |
| | 143 | |
|
| 302 | 144 | | CommonScriptableObjects.rendererState.OnChange += OnRenderingStateChanged; |
| 302 | 145 | | OnRenderingStateChanged(CommonScriptableObjects.rendererState.Get(), false); |
| | 146 | |
|
| 302 | 147 | | if (avatarGameObject == null || firstPersonCameraGameObject == null) |
| | 148 | | { |
| 0 | 149 | | throw new Exception("Both the avatar and first person camera game objects must be set."); |
| | 150 | | } |
| | 151 | |
|
| 302 | 152 | | var worldData = DataStore.i.Get<DataStore_World>(); |
| 302 | 153 | | worldData.avatarTransform.Set(avatarGameObject.transform); |
| 302 | 154 | | worldData.fpsTransform.Set(firstPersonCameraGameObject.transform); |
| | 155 | |
|
| 302 | 156 | | dataStorePlayer.lastTeleportPosition.OnChange += Teleport; |
| 302 | 157 | | } |
| | 158 | |
|
| | 159 | | private void SubscribeToInput() |
| | 160 | | { |
| 302 | 161 | | jumpStartedDelegate = (action) => |
| | 162 | | { |
| 0 | 163 | | lastJumpButtonPressedTime = Time.time; |
| 0 | 164 | | jumpButtonPressed = true; |
| 0 | 165 | | }; |
| 302 | 166 | | jumpFinishedDelegate = (action) => jumpButtonPressed = false; |
| 302 | 167 | | jumpAction.OnStarted += jumpStartedDelegate; |
| 302 | 168 | | jumpAction.OnFinished += jumpFinishedDelegate; |
| | 169 | |
|
| 302 | 170 | | walkStartedDelegate = (action) => isWalking = true; |
| 302 | 171 | | walkFinishedDelegate = (action) => isWalking = false; |
| 302 | 172 | | sprintAction.OnStarted += walkStartedDelegate; |
| 302 | 173 | | sprintAction.OnFinished += walkFinishedDelegate; |
| 302 | 174 | | } |
| | 175 | |
|
| | 176 | | void OnDestroy() |
| | 177 | | { |
| 324 | 178 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 324 | 179 | | jumpAction.OnStarted -= jumpStartedDelegate; |
| 324 | 180 | | jumpAction.OnFinished -= jumpFinishedDelegate; |
| 324 | 181 | | sprintAction.OnStarted -= walkStartedDelegate; |
| 324 | 182 | | sprintAction.OnFinished -= walkFinishedDelegate; |
| 324 | 183 | | CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged; |
| 324 | 184 | | dataStorePlayer.lastTeleportPosition.OnChange -= Teleport; |
| 324 | 185 | | i = null; |
| 324 | 186 | | } |
| | 187 | |
|
| | 188 | | void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 189 | | { |
| 2 | 190 | | Vector3 oldPos = this.transform.position; |
| 2 | 191 | | this.transform.position = characterPosition.unityPosition; //CommonScriptableObjects.playerUnityPosition; |
| | 192 | |
|
| 2 | 193 | | if (CinemachineCore.Instance.BrainCount > 0) |
| | 194 | | { |
| 2 | 195 | | CinemachineCore.Instance.GetActiveBrain(0).ActiveVirtualCamera?.OnTargetObjectWarped(transform, transform.po |
| | 196 | | } |
| 2 | 197 | | } |
| | 198 | |
|
| | 199 | | public void SetPosition(Vector3 newPosition) |
| | 200 | | { |
| | 201 | | // failsafe in case something teleports the player below ground collisions |
| 6093 | 202 | | if (newPosition.y < minimumYPosition) |
| | 203 | | { |
| 75 | 204 | | newPosition.y = minimumYPosition + 2f; |
| | 205 | | } |
| | 206 | |
|
| 6093 | 207 | | lastPosition = characterPosition.worldPosition; |
| 6093 | 208 | | characterPosition.worldPosition = newPosition; |
| 6093 | 209 | | transform.position = characterPosition.unityPosition; |
| 6093 | 210 | | Environment.i.platform.physicsSyncController?.MarkDirty(); |
| | 211 | |
|
| 6093 | 212 | | CommonScriptableObjects.playerUnityPosition.Set(characterPosition.unityPosition); |
| 6093 | 213 | | dataStorePlayer.playerWorldPosition.Set(characterPosition.worldPosition); |
| 6093 | 214 | | Vector2Int playerPosition = Utils.WorldToGridPosition(characterPosition.worldPosition); |
| 6093 | 215 | | CommonScriptableObjects.playerCoords.Set(playerPosition); |
| 6093 | 216 | | dataStorePlayer.playerGridPosition.Set(playerPosition); |
| 6093 | 217 | | dataStorePlayer.playerUnityPosition.Set(characterPosition.unityPosition); |
| | 218 | |
|
| 6093 | 219 | | if (Moved(lastPosition)) |
| | 220 | | { |
| 5380 | 221 | | if (Moved(lastPosition, useThreshold: true)) |
| 5380 | 222 | | ReportMovement(); |
| | 223 | |
|
| 5380 | 224 | | OnCharacterMoved?.Invoke(characterPosition); |
| | 225 | |
|
| 5380 | 226 | | float distance = Vector3.Distance(characterPosition.worldPosition, lastPosition) - movingPlatformSpeed; |
| | 227 | |
|
| 5380 | 228 | | if (distance > 0f && isGrounded) |
| 6 | 229 | | OnMoved?.Invoke(distance); |
| | 230 | | } |
| | 231 | |
|
| 6093 | 232 | | lastPosition = transform.position; |
| 6093 | 233 | | } |
| | 234 | |
|
| | 235 | | public void Teleport(string teleportPayload) |
| | 236 | | { |
| 40 | 237 | | var payload = Utils.FromJsonWithNulls<Vector3>(teleportPayload); |
| 40 | 238 | | dataStorePlayer.lastTeleportPosition.Set(payload, notifyEvent: true); |
| 40 | 239 | | } |
| | 240 | |
|
| | 241 | | private void Teleport(Vector3 newPosition, Vector3 prevPosition) |
| | 242 | | { |
| 41 | 243 | | ResetGround(); |
| | 244 | |
|
| 41 | 245 | | SetPosition(newPosition); |
| | 246 | |
|
| 41 | 247 | | if (OnPositionSet != null) |
| | 248 | | { |
| 0 | 249 | | OnPositionSet.Invoke(characterPosition); |
| | 250 | | } |
| | 251 | |
|
| 41 | 252 | | if (!initialPositionAlreadySet) |
| | 253 | | { |
| 37 | 254 | | initialPositionAlreadySet = true; |
| | 255 | | } |
| 41 | 256 | | } |
| | 257 | |
|
| | 258 | | [Obsolete("SetPosition is deprecated, please use Teleport instead.", true)] |
| 0 | 259 | | public void SetPosition(string positionVector) { Teleport(positionVector); } |
| | 260 | |
|
| 1206 | 261 | | public void SetEnabled(bool enabled) { this.enabled = enabled; } |
| | 262 | |
|
| | 263 | | bool Moved(Vector3 previousPosition, bool useThreshold = false) |
| | 264 | | { |
| 11473 | 265 | | if (useThreshold) |
| 5380 | 266 | | return Vector3.Distance(characterPosition.worldPosition, previousPosition) > 0.001f; |
| | 267 | | else |
| 6093 | 268 | | return characterPosition.worldPosition != previousPosition; |
| | 269 | | } |
| | 270 | |
|
| | 271 | | internal void LateUpdate() |
| | 272 | | { |
| 6051 | 273 | | if(!dataStorePlayer.canPlayerMove.Get()) |
| 0 | 274 | | return; |
| | 275 | |
|
| 6051 | 276 | | if (transform.position.y < minimumYPosition) |
| | 277 | | { |
| 0 | 278 | | SetPosition(characterPosition.worldPosition); |
| 0 | 279 | | return; |
| | 280 | | } |
| | 281 | |
|
| 6051 | 282 | | if (freeMovementController.IsActive()) |
| | 283 | | { |
| 0 | 284 | | velocity = freeMovementController.CalculateMovement(); |
| | 285 | | } |
| | 286 | | else |
| | 287 | | { |
| 6051 | 288 | | velocity.x = 0f; |
| 6051 | 289 | | velocity.z = 0f; |
| 6051 | 290 | | velocity.y += gravity * Time.deltaTime; |
| | 291 | |
|
| 6051 | 292 | | bool previouslyGrounded = isGrounded; |
| | 293 | |
|
| 6051 | 294 | | if (!isJumping || velocity.y <= 0f) |
| 6051 | 295 | | CheckGround(); |
| | 296 | |
|
| 6051 | 297 | | if (isGrounded) |
| | 298 | | { |
| 340 | 299 | | isJumping = false; |
| 340 | 300 | | velocity.y = gravity * Time.deltaTime; // to avoid accumulating gravity in velocity.y while grounded |
| | 301 | | } |
| 5711 | 302 | | else if (previouslyGrounded && !isJumping) |
| | 303 | | { |
| 10 | 304 | | lastUngroundedTime = Time.time; |
| | 305 | | } |
| | 306 | |
|
| 6051 | 307 | | if (characterForward.HasValue()) |
| | 308 | | { |
| | 309 | | // Horizontal movement |
| 34 | 310 | | var speed = movementSpeed * (isWalking ? runningSpeedMultiplier : 1f); |
| | 311 | |
|
| 34 | 312 | | transform.forward = characterForward.Get().Value; |
| | 313 | |
|
| 34 | 314 | | var xzPlaneForward = Vector3.Scale(cameraForward.Get(), new Vector3(1, 0, 1)); |
| 34 | 315 | | var xzPlaneRight = Vector3.Scale(cameraRight.Get(), new Vector3(1, 0, 1)); |
| | 316 | |
|
| 34 | 317 | | Vector3 forwardTarget = Vector3.zero; |
| | 318 | |
|
| 34 | 319 | | if (characterYAxis.GetValue() > CONTROLLER_DRIFT_OFFSET) |
| 0 | 320 | | forwardTarget += xzPlaneForward; |
| 34 | 321 | | if (characterYAxis.GetValue() < -CONTROLLER_DRIFT_OFFSET) |
| 0 | 322 | | forwardTarget -= xzPlaneForward; |
| | 323 | |
|
| 34 | 324 | | if (characterXAxis.GetValue() > CONTROLLER_DRIFT_OFFSET) |
| 0 | 325 | | forwardTarget += xzPlaneRight; |
| 34 | 326 | | if (characterXAxis.GetValue() < -CONTROLLER_DRIFT_OFFSET) |
| 0 | 327 | | forwardTarget -= xzPlaneRight; |
| | 328 | |
|
| 34 | 329 | | if (forwardTarget.Equals(Vector3.zero)) |
| 34 | 330 | | isMovingByUserInput = false; |
| | 331 | | else |
| 0 | 332 | | isMovingByUserInput = true; |
| | 333 | |
|
| | 334 | |
|
| 34 | 335 | | forwardTarget.Normalize(); |
| 34 | 336 | | velocity += forwardTarget * speed; |
| 34 | 337 | | CommonScriptableObjects.playerUnityEulerAngles.Set(transform.eulerAngles); |
| | 338 | | } |
| | 339 | |
|
| 6051 | 340 | | bool jumpButtonPressedWithGraceTime = jumpButtonPressed && (Time.time - lastJumpButtonPressedTime < 0.15f); |
| | 341 | |
|
| 6051 | 342 | | if (jumpButtonPressedWithGraceTime) // almost-grounded jump button press allowed time |
| | 343 | | { |
| 0 | 344 | | bool justLeftGround = (Time.time - lastUngroundedTime) < 0.1f; |
| | 345 | |
|
| 0 | 346 | | if (isGrounded || justLeftGround) // just-left-ground jump allowed time |
| | 347 | | { |
| 0 | 348 | | Jump(); |
| | 349 | | } |
| | 350 | | } |
| | 351 | |
|
| | 352 | | //NOTE(Mordi): Detecting when the character hits the ground (for landing-SFX) |
| 6051 | 353 | | if (isGrounded && !previouslyGrounded && (Time.time - lastUngroundedTime) > 0.4f) |
| | 354 | | { |
| 22 | 355 | | OnHitGround?.Invoke(); |
| | 356 | | } |
| | 357 | | } |
| | 358 | |
|
| 6051 | 359 | | if (characterController.enabled) |
| | 360 | | { |
| | 361 | | //NOTE(Brian): Transform has to be in sync before the Move call, otherwise this call |
| | 362 | | // will reset the character controller to its previous position. |
| 6051 | 363 | | Environment.i.platform.physicsSyncController?.Sync(); |
| 6051 | 364 | | lastCharacterControllerCollision = characterController.Move(velocity * Time.deltaTime); |
| | 365 | | } |
| | 366 | |
|
| 6051 | 367 | | SetPosition(PositionUtils.UnityToWorldPosition(transform.position)); |
| | 368 | |
|
| 6051 | 369 | | if ((DCLTime.realtimeSinceStartup - lastMovementReportTime) > PlayerSettings.POSITION_REPORTING_DELAY) |
| | 370 | | { |
| 72 | 371 | | ReportMovement(); |
| | 372 | | } |
| | 373 | |
|
| 6051 | 374 | | if (isOnMovingPlatform) |
| | 375 | | { |
| 0 | 376 | | SaveLateUpdateGroundTransforms(); |
| | 377 | | } |
| 6051 | 378 | | OnUpdateFinish?.Invoke(Time.deltaTime); |
| 0 | 379 | | } |
| | 380 | |
|
| | 381 | | private void SaveLateUpdateGroundTransforms() |
| | 382 | | { |
| 0 | 383 | | lastLocalGroundPosition = groundTransform.InverseTransformPoint(transform.position); |
| | 384 | |
|
| 0 | 385 | | if (CommonScriptableObjects.characterForward.HasValue()) |
| | 386 | | { |
| 0 | 387 | | lastCharacterRotation = groundTransform.InverseTransformDirection(CommonScriptableObjects.characterForward.G |
| 0 | 388 | | lastGlobalCharacterRotation = CommonScriptableObjects.characterForward.Get().Value; |
| | 389 | | } |
| 0 | 390 | | } |
| | 391 | |
|
| | 392 | | void Jump() |
| | 393 | | { |
| 0 | 394 | | if (isJumping) |
| 0 | 395 | | return; |
| | 396 | |
|
| 0 | 397 | | isJumping = true; |
| 0 | 398 | | isGrounded = false; |
| | 399 | |
|
| 0 | 400 | | ResetGround(); |
| | 401 | |
|
| 0 | 402 | | velocity.y = jumpForce; |
| | 403 | | //cameraTargetProbe.damping.y = dampingOnAir; |
| | 404 | |
|
| 0 | 405 | | OnJump?.Invoke(); |
| 0 | 406 | | } |
| | 407 | |
|
| | 408 | | public void ResetGround() |
| | 409 | | { |
| 11482 | 410 | | if (isOnMovingPlatform) |
| 0 | 411 | | CommonScriptableObjects.playerIsOnMovingPlatform.Set(false); |
| | 412 | |
|
| 11482 | 413 | | isOnMovingPlatform = false; |
| 11482 | 414 | | groundTransform = null; |
| 11482 | 415 | | movingPlatformSpeed = 0; |
| 11482 | 416 | | } |
| | 417 | |
|
| | 418 | | void CheckGround() |
| | 419 | | { |
| 6051 | 420 | | if (groundTransform == null) |
| 5728 | 421 | | ResetGround(); |
| | 422 | |
|
| 6051 | 423 | | if (isOnMovingPlatform) |
| | 424 | | { |
| 0 | 425 | | Physics.SyncTransforms(); |
| | 426 | | //NOTE(Brian): This should move the character with the moving platform |
| 0 | 427 | | Vector3 newGroundWorldPos = groundTransform.TransformPoint(lastLocalGroundPosition); |
| 0 | 428 | | movingPlatformSpeed = Vector3.Distance(newGroundWorldPos, transform.position); |
| 0 | 429 | | transform.position = newGroundWorldPos; |
| | 430 | |
|
| 0 | 431 | | Vector3 newCharacterForward = groundTransform.TransformDirection(lastCharacterRotation); |
| 0 | 432 | | Vector3 lastFrameDifference = Vector3.zero; |
| 0 | 433 | | if (CommonScriptableObjects.characterForward.HasValue()) |
| | 434 | | { |
| 0 | 435 | | lastFrameDifference = CommonScriptableObjects.characterForward.Get().Value - lastGlobalCharacterRotation |
| | 436 | | } |
| | 437 | |
|
| | 438 | | //NOTE(Kinerius) CameraStateTPS rotates the character between frames so we add the difference. |
| | 439 | | // if we dont do this, the character wont rotate when moving, only when the platform rotates |
| 0 | 440 | | var newForward = newCharacterForward + lastFrameDifference; |
| | 441 | |
|
| 0 | 442 | | if (newForward is { x: 0, y: 0, z: 0 }) |
| 0 | 443 | | newForward = Vector3.forward; |
| | 444 | |
|
| 0 | 445 | | CommonScriptableObjects.characterForward.Set(newForward); |
| | 446 | | } |
| | 447 | |
|
| 6051 | 448 | | Transform transformHit = CastGroundCheckingRays(); |
| | 449 | |
|
| 6051 | 450 | | if (transformHit != null) |
| | 451 | | { |
| 338 | 452 | | if (groundTransform == transformHit) |
| | 453 | | { |
| 315 | 454 | | bool groundHasMoved = (transformHit.position != groundLastPosition || transformHit.rotation != groundLas |
| | 455 | |
|
| 315 | 456 | | if (!characterPosition.RepositionedWorldLastFrame() |
| | 457 | | && groundHasMoved) |
| | 458 | | { |
| 0 | 459 | | isOnMovingPlatform = true; |
| 0 | 460 | | CommonScriptableObjects.playerIsOnMovingPlatform.Set(true); |
| 0 | 461 | | Physics.SyncTransforms(); |
| 0 | 462 | | SaveLateUpdateGroundTransforms(); |
| | 463 | |
|
| 0 | 464 | | Quaternion deltaRotation = groundTransform.rotation * Quaternion.Inverse(groundLastRotation); |
| 0 | 465 | | CommonScriptableObjects.movingPlatformRotationDelta.Set(deltaRotation); |
| | 466 | | } |
| | 467 | | } |
| | 468 | | else |
| | 469 | | { |
| 23 | 470 | | groundTransform = transformHit; |
| 23 | 471 | | CommonScriptableObjects.movingPlatformRotationDelta.Set(Quaternion.identity); |
| | 472 | | } |
| | 473 | | } |
| | 474 | | else |
| | 475 | | { |
| 5713 | 476 | | ResetGround(); |
| | 477 | | } |
| | 478 | |
|
| 6051 | 479 | | if (groundTransform != null) |
| | 480 | | { |
| 338 | 481 | | groundLastPosition = groundTransform.position; |
| 338 | 482 | | groundLastRotation = groundTransform.rotation; |
| | 483 | | } |
| | 484 | |
|
| 6051 | 485 | | isGrounded = IsLastCollisionGround() || groundTransform != null && groundTransform.gameObject.activeInHierarchy; |
| 6051 | 486 | | } |
| | 487 | |
|
| | 488 | | public Transform CastGroundCheckingRays() |
| | 489 | | { |
| | 490 | | RaycastHit hitInfo; |
| | 491 | |
|
| 6051 | 492 | | var result = CastGroundCheckingRays(transform, collider, groundCheckExtraDistance, 0.9f, groundLayers, out hitIn |
| | 493 | |
|
| 6051 | 494 | | if ( result ) |
| | 495 | | { |
| 338 | 496 | | return hitInfo.transform; |
| | 497 | | } |
| | 498 | |
|
| 5713 | 499 | | return null; |
| | 500 | | } |
| | 501 | |
|
| | 502 | | public bool CastGroundCheckingRays(float extraDistance, float scale, out RaycastHit hitInfo) |
| | 503 | | { |
| 6005 | 504 | | if (CastGroundCheckingRays(transform, collider, extraDistance, scale, groundLayers | cameraLayers , out hitInfo) |
| 425 | 505 | | return true; |
| | 506 | |
|
| 5580 | 507 | | return IsLastCollisionGround(); |
| | 508 | | } |
| | 509 | |
|
| | 510 | | public bool CastGroundCheckingRay(float extraDistance, out RaycastHit hitInfo) |
| | 511 | | { |
| 0 | 512 | | Bounds bounds = collider.bounds; |
| 0 | 513 | | float rayMagnitude = (bounds.extents.y + extraDistance); |
| 0 | 514 | | bool test = CastGroundCheckingRay(transform.position, out hitInfo, rayMagnitude, groundLayers); |
| 0 | 515 | | return IsLastCollisionGround() || test; |
| | 516 | | } |
| | 517 | |
|
| | 518 | | // We secuentially cast rays in 4 directions (only if the previous one didn't hit anything) |
| | 519 | | public static bool CastGroundCheckingRays(Transform transform, Collider collider, float extraDistance, float scale, |
| | 520 | | { |
| 12056 | 521 | | Bounds bounds = collider.bounds; |
| | 522 | |
|
| 12056 | 523 | | float rayMagnitude = (bounds.extents.y + extraDistance); |
| 12056 | 524 | | float originScale = scale * bounds.extents.x; |
| | 525 | |
|
| 12056 | 526 | | if (!CastGroundCheckingRay(transform.position, out hitInfo, rayMagnitude, groundLayers) // center |
| | 527 | | && !CastGroundCheckingRay( transform.position + transform.forward * originScale, out hitInfo, rayMagnitude, |
| | 528 | | && !CastGroundCheckingRay( transform.position + transform.right * originScale, out hitInfo, rayMagnitude, gr |
| | 529 | | && !CastGroundCheckingRay( transform.position + -transform.forward * originScale, out hitInfo, rayMagnitude, |
| | 530 | | && !CastGroundCheckingRay( transform.position + -transform.right * originScale, out hitInfo, rayMagnitude, g |
| | 531 | | { |
| 11293 | 532 | | return false; |
| | 533 | | } |
| | 534 | |
|
| | 535 | | // At this point there is a guaranteed hit, so this is not null |
| 763 | 536 | | return true; |
| | 537 | | } |
| | 538 | |
|
| | 539 | | public static bool CastGroundCheckingRay(Vector3 origin, out RaycastHit hitInfo, float rayMagnitude, int groundLayer |
| | 540 | | { |
| 57228 | 541 | | var ray = new Ray(); |
| 57228 | 542 | | ray.origin = origin; |
| 57228 | 543 | | ray.direction = Vector3.down * rayMagnitude; |
| | 544 | |
|
| 57228 | 545 | | var result = Physics.Raycast(ray, out hitInfo, rayMagnitude, groundLayers); |
| | 546 | |
|
| | 547 | | #if UNITY_EDITOR |
| 57228 | 548 | | if ( result ) |
| 763 | 549 | | Debug.DrawLine(ray.origin, hitInfo.point, Color.green); |
| | 550 | | else |
| 56465 | 551 | | Debug.DrawRay(ray.origin, ray.direction, Color.red); |
| | 552 | | #endif |
| | 553 | |
|
| 56465 | 554 | | return result; |
| | 555 | | } |
| | 556 | |
|
| | 557 | | public void ReportMovement() |
| | 558 | | { |
| 5452 | 559 | | var reportPosition = characterPosition.worldPosition; |
| 5452 | 560 | | var compositeRotation = Quaternion.LookRotation(characterForward.HasValue() ? characterForward.Get().Value : cam |
| 5452 | 561 | | var cameraRotation = Quaternion.LookRotation(cameraForward.Get()); |
| | 562 | |
|
| | 563 | | //NOTE(Brian): We have to wait for a Teleport before sending the ReportPosition, because if not ReportPosition e |
| | 564 | | // When the spawn point is being selected / scenes being prepared to be sent and the Kernel gets cra |
| | 565 | |
|
| | 566 | | // The race conditions that can arise from not having this flag can result in: |
| | 567 | | // - Scenes not being sent for loading, making ActivateRenderer never being sent, only in WSS m |
| | 568 | | // - Random teleports to 0,0 or other positions that shouldn't happen. |
| 5452 | 569 | | if (initialPositionAlreadySet) |
| 77 | 570 | | WebInterface.ReportPosition(reportPosition, compositeRotation, characterController.height, cameraRotation); |
| | 571 | |
|
| 5452 | 572 | | lastMovementReportTime = DCLTime.realtimeSinceStartup; |
| 5452 | 573 | | } |
| | 574 | |
|
| | 575 | | public void PauseGravity() |
| | 576 | | { |
| 40 | 577 | | gravity = 0f; |
| 40 | 578 | | velocity.y = 0f; |
| 40 | 579 | | } |
| | 580 | |
|
| 0 | 581 | | public void ResumeGravity() { gravity = originalGravity; } |
| | 582 | |
|
| | 583 | | void OnRenderingStateChanged(bool isEnable, bool prevState) |
| | 584 | | { |
| 603 | 585 | | SetEnabled(isEnable && !DataStore.i.common.isSignUpFlow.Get()); |
| 603 | 586 | | } |
| | 587 | |
|
| | 588 | | bool IsLastCollisionGround() |
| | 589 | | { |
| 11631 | 590 | | return (lastCharacterControllerCollision & CollisionFlags.Below) != 0; |
| | 591 | | } |
| | 592 | | } |