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