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