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