瀏覽代碼

Character movement and stamina system made

Priz 1 周之前
父節點
當前提交
3f39e44bdf

+ 5 - 1
GAMEN3-ValleyRunner_SanTi036/Assets/Scenes/SampleScene.unity

@@ -685,9 +685,13 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 3ba26df39988ff64090fe13643966bf6, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
-  moveSpeed: 6
+  walkSpeed: 6
+  sprintSpeed: 10
   gravity: -9.81
   jumpHeight: 2
+  mouseSensitivity: 200
+  cameraHolder: {fileID: 330585546}
+  Stamina: 5
 --- !u!1660057539 &9223372036854775807
 SceneRoots:
   m_ObjectHideFlags: 0

+ 79 - 3
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/PlayerController.cs

@@ -1,11 +1,28 @@
 using UnityEngine;
+using UnityEngine.Rendering;
 
-public class PlayerController: MonoBehaviour
+public class PlayerController : MonoBehaviour
 {
-    public float moveSpeed = 6f;
+    [Header("Movement")]
+    public float walkSpeed = 6f;
+    public float sprintSpeed = 10f;
+    private float currentSpeed;
+
+    [Header("Verticality")]
     public float gravity = -9.81f;
     public float jumpHeight = 2f;
 
+    [Header("Camera")]
+    public float mouseSensitivity = 200f;
+    public Transform cameraHolder;
+    private float xRotation = 0f;
+
+    [Header("Stamina")]
+    public int Stamina = 5;
+    public int maxStamina = 5;
+    private float staminaTimer = 0f;
+    private float regenTimer = 0f;
+
     private CharacterController controller;
     private Vector3 velocity;
     private bool isGrounded;
@@ -13,10 +30,52 @@ public class PlayerController: MonoBehaviour
     void Start()
     {
         controller = GetComponent<CharacterController>();
+
+        Cursor.lockState = CursorLockMode.Locked;
     }
 
     void Update()
     {
+        float x = Input.GetAxis("Horizontal");
+        float y = Input.GetAxis("Vertical");
+        bool isMoving = (x != 0 || y != 0);
+        bool isSprinting = Input.GetKey(KeyCode.LeftShift);
+
+        if (!isMoving && !isSprinting && Stamina < maxStamina)
+        {
+            regenTimer += Time.deltaTime;
+
+            if (regenTimer >= 1f) {
+                Stamina++;
+                regenTimer = 0f;
+            }
+        }
+        else
+        {
+            regenTimer = 0f;
+        }
+
+        Camera();
+        Movement();
+    }
+
+    void Camera()
+    {
+        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
+        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
+
+        transform.Rotate(Vector3.up * mouseX);
+
+        xRotation -= mouseY;
+        xRotation = Mathf.Clamp(xRotation, -80f, 80f);
+
+        cameraHolder.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
+    }
+
+    void Movement()
+    {
+        bool isSprinting = Input.GetKey(KeyCode.LeftShift);
+
         isGrounded = controller.isGrounded;
         if (isGrounded && velocity.y < 0)
         {
@@ -27,7 +86,24 @@ public class PlayerController: MonoBehaviour
         float z = Input.GetAxis("Vertical");
         Vector3 move = transform.right * x + transform.forward * z;
 
-        controller.Move(move * moveSpeed * Time.deltaTime);
+        if (isSprinting && Stamina > 0)
+        {
+            currentSpeed = sprintSpeed;
+            staminaTimer += Time.deltaTime;
+
+            if (staminaTimer >= 0.5f)
+            {
+                Stamina--;
+                staminaTimer = 0f;
+            }
+        }
+        else
+        {
+            currentSpeed = walkSpeed;
+            staminaTimer = 0f;
+        }
+
+        controller.Move(move * currentSpeed * Time.deltaTime);
 
         if (Input.GetButtonDown("Jump") && isGrounded)
         {