File size: 18,063 Bytes
d8504ac 12fea46 d8504ac 12fea46 41be7b6 12fea46 60b1f3d adfc8fc 60b1f3d adfc8fc 60b1f3d 12fea46 ba3c4f4 e150f6a d8504ac e150f6a 12fea46 ba3c4f4 d8504ac 8ad6fca dfdb6c1 d8504ac 12fea46 04b1a9b 3cbae3a 92a1fc0 3cbae3a d8504ac 6e5e518 dfdb6c1 04b1a9b 92a1fc0 6e5e518 04b1a9b e516659 8ad6fca ffe1596 6e5e518 ffe1596 e516659 0d8e3e4 e516659 aa3961d e516659 aa3961d 8ad6fca 60b1f3d e516659 6e5e518 60eed86 aa3961d e1cb763 aa3961d e1cb763 792f92b 41be7b6 aa3961d 41be7b6 d8504ac 12fea46 e150f6a 12fea46 92a1fc0 3cbae3a ba3c4f4 aa3961d d8504ac e150f6a 41be7b6 792f92b e1cb763 aa3961d 12fea46 ba3c4f4 aa3961d 12fea46 41be7b6 e516659 aa3961d 013a543 d8504ac 12fea46 aa3961d e516659 d8504ac e516659 aa3961d d8504ac e516659 d8504ac aa3961d e516659 ffe1596 aa3961d ffe1596 60eed86 ffe1596 aa3961d 60b1f3d aa3961d 60b1f3d 12fea46 ba3c4f4 e516659 e150f6a ba3c4f4 e150f6a d8504ac ba3c4f4 d8504ac ba3c4f4 aa3961d 12fea46 92a1fc0 e150f6a 41be7b6 e150f6a 6e5e518 60eed86 e516659 60eed86 e516659 60eed86 e516659 60eed86 6e5e518 92a1fc0 6e5e518 e516659 6e5e518 e516659 6e5e518 e516659 6e5e518 e516659 8ad6fca e516659 6e5e518 92a1fc0 12fea46 e516659 aa3961d 8ad6fca aa3961d e516659 aa3961d 0d8e3e4 aa3961d 60eed86 aa3961d e516659 ffe1596 aa3961d e516659 aa3961d e516659 8ad6fca e516659 8ad6fca e1cb763 60b1f3d adfc8fc e516659 60b1f3d e516659 e1cb763 60b1f3d e516659 e1cb763 e516659 8ad6fca e516659 8ad6fca e516659 41be7b6 792f92b aa3961d 792f92b 41be7b6 12fea46 3cbae3a 12fea46 e150f6a 3cbae3a d8504ac e150f6a d8504ac ba3c4f4 92a1fc0 e150f6a 92a1fc0 12fea46 d8504ac 3cbae3a 013a543 12fea46 e150f6a 12fea46 e150f6a 12fea46 04b1a9b 92a1fc0 8ad6fca 04b1a9b d8504ac 92a1fc0 60eed86 ffe1596 60eed86 ffe1596 60eed86 ffe1596 60eed86 aa3961d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.Animations;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
// --- Climb Events (for external systems like a hand/IK/animation controller) ---
// Fired when the climb prompt becomes visible/hidden (i.e. player is looking at a climbable wall while airborne).
public event Action<bool> OnClimbPromptChanged;
// Fired the moment the player actually sticks to the wall (after the leap finishes).
// Passes the wall hit point and wall normal so external systems (e.g. hand IK) can position themselves.
public event Action<Vector3, Vector3> OnWallStickStarted;
// Fired the moment wall sticking ends, for ANY reason (grounded, key toggle, wall jump, or timer running out).
public event Action OnWallStickEnded;
// Current normalized stick time remaining (1 = full, 0 = about to fall). Read-only for consumers.
public float StickTimeRemaining01 => maxStickTime > 0f ? Mathf.Clamp01(stickTimer / maxStickTime) : 0f;
public bool IsWallSticking => isWallSticking;
public Transform Orient => orient;
[Header("Movement")]
public float baseMoveSpeed = 8f;
public float sprintMoveSpeed = 12f;
[Tooltip("How fast your target speed transitions between walking and sprinting.")]
public float sprintTransitionSpeed = 10f;
public KeyCode SprintKey = KeyCode.LeftShift;
private float targetMoveSpeed;
private float currentMaxSpeed;
public Transform orient;
[Header("Acceleration Settings")]
public float normalAcceleration = 5f;
public float iceAcceleration = 2f;
private float currentAcceleration;
[Header("Friction & Braking")]
public float normalGroundDrag = 0f;
public float iceGroundDrag = 0f;
[Header("Smooth Stop Settings")]
[Tooltip("Higher numbers = stops faster. Lower numbers = slides further")]
public float normalBrakingSpeed = 15f;
public float iceBrakingSpeed = 0.5f;
private float currentBrakingSpeed;
[Header("Check Ground")]
public float playerHeight = 2;
public LayerMask whatIsGound;
bool grounded;
[Header("Jump Setting")]
public float jumpForce = 13f;
public float jumpCoolDown = 0.25f;
public float gravityMultiplier = 2.5f;
[Range(0f, 1f)] public float airMultiplier = 0.1f;
[Tooltip("How long (in seconds) the game remembers your jump press before you hit the ground.")]
public float jumpBufferTime = 0.35f;
bool ableToJump;
public KeyCode JumpKey = KeyCode.Space;
private float jumpInputBufferTimer;
[Header("Climb / Wall Stick Settings")]
public KeyCode ClimbKey = KeyCode.Mouse0;
[Tooltip("Distance from chest view to detect climbable wall.")]
public float wallCheckDistance = 2.5f;
[Tooltip("How wide the detection sphere is. Covers torso and head when centered at chest level.")]
public float wallCheckRadius = 0.8f;
[Tooltip("Time in seconds the player can hang before falling")]
public float maxStickTime = 5f;
[Tooltip("How far the player's pivot stays from the wall surface while wall-sticking.")]
public float wallStickDistance = 0.5f;
private bool isWallSticking;
private bool isLeapingToWall;
private float stickTimer;
private Vector3 wallNormal;
private Vector3 wallHitPoint;
private bool isTouchingClimbableWall;
private bool lastClimbPromptState;
// Input buffering window for climbing (0.25s)
private float climbInputBufferTimer;
[Header("Climb Prompt UI")]
[Tooltip("Assign your UI Panel (positioned at the center of your screen Canvas)")]
public GameObject climbPromptUI;
private Camera mainCamera;
[Header("Wall Timer UI")]
public Slider wallTimerSlider;
public Image wallTimerFillImage;
public Color normalTimerColor = Color.white;
public Color lowTimerColor = Color.red;
public float colorTransitionSpeed = 5f;
private Color targetTimerColor;
[Header("Stamina Setting & UI")]
public Slider staminaSlider;
public Image staminaFillImage;
public Color normalStaminaColor = Color.white;
public Color lowStaminaColor = Color.red;
private Color targetColor;
public float maxStamina = 100f;
public float staminaDrainRate = 25f;
public float staminaRegenRate = 15f;
private float currentStamina;
private bool isExhausted;
[Header("UI Display")]
public TextMeshProUGUI velocityText;
float hozInput;
float verInput;
Vector3 direction;
bool isSprinting;
Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
ableToJump = true;
rb.useGravity = false;
mainCamera = Camera.main;
targetMoveSpeed = baseMoveSpeed;
currentMaxSpeed = baseMoveSpeed;
currentStamina = maxStamina;
if (staminaSlider != null)
{
staminaSlider.maxValue = maxStamina;
staminaSlider.value = maxStamina;
}
if (staminaFillImage != null)
{
staminaFillImage.color = normalStaminaColor;
}
if (wallTimerSlider != null)
{
wallTimerSlider.maxValue = maxStickTime;
wallTimerSlider.value = maxStickTime;
wallTimerSlider.gameObject.SetActive(false);
}
if (climbPromptUI != null)
{
climbPromptUI.SetActive(false);
}
}
private void Update()
{
HandleGroundCheckAndFriction();
CheckClimbableWall();
MyInput();
HandleStamina();
HandleWallStickTimer();
HandleClimbPromptUI();
SpeedController();
UpdateVelocityUI();
}
private void FixedUpdate()
{
if (!isWallSticking && !isLeapingToWall)
{
MovePlayer();
ApplyCustomGravity();
if (hozInput == 0 && verInput == 0 && grounded)
{
DeceleratePlayer();
}
}
else if (isWallSticking)
{
rb.velocity = Vector3.zero;
}
}
private void CheckClimbableWall()
{
if (mainCamera == null) mainCamera = Camera.main;
// CENTER AT CHEST LEVEL (Half of total player height)
Vector3 chestOrigin = transform.position + Vector3.up * (playerHeight * 0.5f);
Vector3 rayDirection = mainCamera != null ? mainCamera.transform.forward : orient.forward;
// Pullback origin so the sphere doesn't start inside walls on high-speed impact
chestOrigin -= rayDirection * wallCheckRadius;
float actualCheckDistance = wallCheckDistance + wallCheckRadius;
if (Physics.SphereCast(chestOrigin, wallCheckRadius, rayDirection, out RaycastHit hit, actualCheckDistance))
{
if (hit.collider.CompareTag("Climbable"))
{
isTouchingClimbableWall = true;
wallNormal = hit.normal;
wallHitPoint = hit.point;
return;
}
}
isTouchingClimbableWall = false;
}
private void HandleClimbPromptUI()
{
bool showPrompt = !grounded && isTouchingClimbableWall && !isWallSticking && !isLeapingToWall;
if (climbPromptUI != null && climbPromptUI.activeSelf != showPrompt)
{
climbPromptUI.SetActive(showPrompt);
}
if (showPrompt != lastClimbPromptState)
{
lastClimbPromptState = showPrompt;
OnClimbPromptChanged?.Invoke(showPrompt);
}
}
private void HandleGroundCheckAndFriction()
{
float sphereRadius = 0.4f;
float castDistance = (playerHeight * 0.5f) - sphereRadius + 0.2f;
RaycastHit hit;
grounded = Physics.SphereCast(transform.position, sphereRadius, Vector3.down, out hit, castDistance, whatIsGound);
if (grounded)
{
if (isWallSticking) EndWallStick();
float surfaceMultiplier = hit.collider.CompareTag("Ice") ? 1.3f : 1.0f;
float desiredSpeedLimit = isSprinting ? sprintMoveSpeed : baseMoveSpeed;
targetMoveSpeed = desiredSpeedLimit * surfaceMultiplier;
if (hit.collider.CompareTag("Ice"))
{
rb.drag = iceGroundDrag;
currentBrakingSpeed = iceBrakingSpeed;
currentAcceleration = iceAcceleration;
}
else
{
rb.drag = normalGroundDrag;
currentBrakingSpeed = normalBrakingSpeed;
currentAcceleration = normalAcceleration;
}
}
else
{
rb.drag = 0.5f;
}
}
private void MyInput()
{
hozInput = Input.GetAxisRaw("Horizontal");
verInput = Input.GetAxisRaw("Vertical");
bool isMovingStraight = (hozInput != 0 && verInput == 0) || (verInput != 0 && hozInput == 0);
if (Input.GetKey(SprintKey) && isMovingStraight && grounded && !isExhausted)
{
isSprinting = true;
}
else
{
isSprinting = false;
}
// --- CLIMB INPUT BUFFERING ---
if (climbInputBufferTimer > 0)
{
climbInputBufferTimer -= Time.deltaTime;
}
if (Input.GetKeyDown(ClimbKey))
{
if (isWallSticking)
{
EndWallStick();
}
else
{
climbInputBufferTimer = 0.25f;
}
}
if (climbInputBufferTimer > 0 && !grounded && isTouchingClimbableWall && !isLeapingToWall && !isWallSticking)
{
climbInputBufferTimer = 0f;
StartCoroutine(LeapToWallRoutine(wallHitPoint, wallNormal));
}
// --- JUMP INPUT BUFFERING ---
if (jumpInputBufferTimer > 0)
{
jumpInputBufferTimer -= Time.deltaTime;
}
if (Input.GetKeyDown(JumpKey))
{
if (grounded && ableToJump && !isLeapingToWall)
{
// Normal Ground Jump
ableToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCoolDown);
}
else if (isWallSticking && ableToJump && !isLeapingToWall)
{
// Wall Jump
ableToJump = false;
EndWallStick();
WallJump();
Invoke(nameof(ResetJump), jumpCoolDown);
}
else if (!grounded && !isWallSticking)
{
// We are mid-air. Start the buffer timer to remember the jump press.
jumpInputBufferTimer = jumpBufferTime;
}
}
// Check if we hit the ground while our jump memory is still active
if (jumpInputBufferTimer > 0 && grounded && ableToJump && !isLeapingToWall)
{
jumpInputBufferTimer = 0f; // Consume the buffer
ableToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCoolDown);
}
}
private IEnumerator LeapToWallRoutine(Vector3 hitPoint, Vector3 normal)
{
isLeapingToWall = true;
Vector3 startPos = transform.position;
Vector3 targetPos = hitPoint + (normal * wallStickDistance);
float dashSpeed = Mathf.Max(rb.velocity.magnitude, baseMoveSpeed * 1.5f);
float distance = Vector3.Distance(startPos, targetPos);
float duration = distance / dashSpeed;
float elapsed = 0f;
while (elapsed < duration)
{
rb.velocity = Vector3.zero;
elapsed += Time.deltaTime;
transform.position = Vector3.Lerp(startPos, targetPos, elapsed / duration);
yield return null;
}
transform.position = targetPos;
isLeapingToWall = false;
StartWallStick();
}
private void StartWallStick()
{
isWallSticking = true;
stickTimer = maxStickTime;
rb.isKinematic = false;
rb.velocity = Vector3.zero;
if (wallTimerSlider != null)
{
wallTimerSlider.gameObject.SetActive(true);
wallTimerSlider.value = maxStickTime;
}
if (wallTimerFillImage != null)
{
wallTimerFillImage.color = normalTimerColor;
}
OnWallStickStarted?.Invoke(wallHitPoint, wallNormal);
}
private void EndWallStick()
{
if (!isWallSticking) return;
isWallSticking = false;
if (wallTimerSlider != null)
{
wallTimerSlider.gameObject.SetActive(false);
}
OnWallStickEnded?.Invoke();
}
private void HandleWallStickTimer()
{
if (!isWallSticking) return;
stickTimer -= Time.deltaTime;
if (wallTimerSlider != null)
{
wallTimerSlider.value = stickTimer;
}
if (stickTimer <= (maxStickTime * 0.5f))
{
targetTimerColor = lowTimerColor;
}
else
{
targetTimerColor = normalTimerColor;
}
if (wallTimerFillImage != null)
{
wallTimerFillImage.color = Color.Lerp(wallTimerFillImage.color, targetTimerColor, colorTransitionSpeed * Time.deltaTime);
}
if (stickTimer <= 0f)
{
EndWallStick();
}
}
private void WallJump()
{
EndWallStick();
Vector3 forceDirection = transform.up + (wallNormal * 0.1f);
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(forceDirection.normalized * jumpForce, ForceMode.Impulse);
}
private void HandleStamina()
{
if (staminaSlider != null)
{
staminaSlider.value = currentStamina;
}
if (isSprinting)
{
currentStamina -= staminaDrainRate * Time.deltaTime;
if (currentStamina <= 0f)
{
currentStamina = 0f;
isSprinting = false;
isExhausted = true;
}
}
else
{
currentStamina += staminaRegenRate * Time.deltaTime;
if (currentStamina > maxStamina) currentStamina = maxStamina;
if (isExhausted && currentStamina >= (maxStamina * 0.2f))
{
isExhausted = false;
}
}
if (currentStamina <= (maxStamina * 0.2f))
{
targetColor = lowStaminaColor;
}
else
{
targetColor = normalStaminaColor;
}
if (staminaFillImage != null)
{
staminaFillImage.color = Color.Lerp(staminaFillImage.color, targetColor, colorTransitionSpeed * Time.deltaTime);
}
if (staminaSlider != null)
{
staminaSlider.value = currentStamina;
}
}
private void MovePlayer()
{
direction = orient.right * hozInput + orient.forward * verInput;
currentMaxSpeed = Mathf.MoveTowards(currentMaxSpeed, targetMoveSpeed, sprintTransitionSpeed * Time.fixedDeltaTime);
if (grounded)
{
rb.AddForce(direction.normalized * currentMaxSpeed * currentAcceleration, ForceMode.Force);
}
else
{
rb.AddForce(direction.normalized * currentMaxSpeed * 10f * airMultiplier, ForceMode.Force);
}
}
private void DeceleratePlayer()
{
Vector3 horizontalVelocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
Vector3 smoothedVelocity = Vector3.MoveTowards(horizontalVelocity, Vector3.zero, currentBrakingSpeed * Time.fixedDeltaTime);
rb.velocity = new Vector3(smoothedVelocity.x, rb.velocity.y, smoothedVelocity.z);
}
private void ApplyCustomGravity()
{
if (!grounded)
{
rb.AddForce(Vector3.down * (9.81f * gravityMultiplier), ForceMode.Force);
}
else
{
rb.AddForce(Vector3.down * 9.81f, ForceMode.Force);
}
}
private void SpeedController()
{
Vector3 flatVeclocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
if (flatVeclocity.magnitude > currentMaxSpeed)
{
Vector3 limitedSpeed = flatVeclocity.normalized * currentMaxSpeed;
rb.velocity = new Vector3(limitedSpeed.x, rb.velocity.y, limitedSpeed.z);
}
}
private void Jump()
{
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void UpdateVelocityUI()
{
if (velocityText != null)
{
Vector3 flatVelocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
float speed = flatVelocity.magnitude;
velocityText.text = "Speed: " + speed.ToString("F1") + " m/s";
}
}
private void ResetJump()
{
ableToJump = true;
}
private void OnDrawGizmos()
{
if (!Application.isPlaying) return;
// Gizmo aligned with chest level origin
Vector3 chestOrigin = transform.position + Vector3.up * (playerHeight * 0.5f);
Vector3 rayDirection = mainCamera != null ? mainCamera.transform.forward : orient != null ? orient.forward : transform.forward;
chestOrigin -= rayDirection * wallCheckRadius;
float actualCheckDistance = wallCheckDistance + wallCheckRadius;
Gizmos.color = isTouchingClimbableWall ? new Color(0, 1, 0, 0.4f) : new Color(1, 0, 0, 0.4f);
Gizmos.DrawWireSphere(chestOrigin, wallCheckRadius);
Gizmos.DrawLine(chestOrigin, chestOrigin + rayDirection * actualCheckDistance);
if (isTouchingClimbableWall)
{
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(wallHitPoint, 0.15f);
Gizmos.DrawRay(wallHitPoint, wallNormal * 0.5f);
}
}
} |