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