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