From 83f3af4baf38b8b25a6e73e19ea2c868bfe0c6f7 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Tue, 20 Jun 2023 20:13:35 -0700 Subject: [PATCH] Movement changes --- playtest/Assets/Scripts/PlayerController.cs | 89 +++++++++++++-------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/playtest/Assets/Scripts/PlayerController.cs b/playtest/Assets/Scripts/PlayerController.cs index b0b8e7e..07ec954 100644 --- a/playtest/Assets/Scripts/PlayerController.cs +++ b/playtest/Assets/Scripts/PlayerController.cs @@ -6,20 +6,17 @@ using UnityEngine; public class FPSController : MonoBehaviour { public Camera playerCamera; - public float walkSpeed = 6f; - public float runSpeed = 12f; + public float walkSpeed = 9f; + public float runSpeed = 18f; public float jumpPower = 7f; public float gravity = 20f; public float lookSpeed = 2f; public float lookXLimit = 45f; - public float bhopMultiplierIncrement = 0.05f; - public float maxBhopSpeed = 20f; + public float airAcceleration = 2f; + public float groundFriction = 6f; - Vector3 moveDirection = Vector3.zero; + Vector3 velocity = Vector3.zero; float rotationX = 0; - float bhopMultiplier = 1f; - bool isJumping = false; - bool wasRunning = false; public bool canMove = true; @@ -34,38 +31,52 @@ public class FPSController : MonoBehaviour void Update() { - Vector3 forward = transform.TransformDirection(Vector3.forward); - Vector3 right = transform.TransformDirection(Vector3.right); - bool isRunning = characterController.isGrounded ? Input.GetKey(KeyCode.LeftShift) : wasRunning; - wasRunning = isRunning; - float curSpeedX = canMove ? ((isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical")) * bhopMultiplier : 0; - float curSpeedY = canMove ? ((isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal")) * bhopMultiplier : 0; - float movementDirectionY = moveDirection.y; - moveDirection = (forward * curSpeedX) + (right * curSpeedY); + Vector3 forward = playerCamera.transform.TransformDirection(Vector3.forward); + Vector3 right = playerCamera.transform.TransformDirection(Vector3.right); + forward.y = 0; + right.y = 0; + forward.Normalize(); + right.Normalize(); - if (Input.GetButton("Jump") && canMove && characterController.isGrounded) + bool isRunning = Input.GetKey(KeyCode.LeftShift); + float speed = isRunning ? runSpeed : walkSpeed; + Vector3 wishDir = forward * Input.GetAxis("Vertical") + right * Input.GetAxis("Horizontal"); + + if (characterController.isGrounded) { - moveDirection.y = jumpPower; - bhopMultiplier = Mathf.Min(bhopMultiplier + bhopMultiplierIncrement, maxBhopSpeed / (isRunning ? runSpeed : walkSpeed)); - isJumping = true; + velocity.y = -0.5f; // Stick to the ground + + if (canMove) + { + float drop = velocity.magnitude * groundFriction * Time.deltaTime; + velocity *= Mathf.Max(velocity.magnitude - drop, 0) / velocity.magnitude; // Apply friction + + Accelerate(wishDir, speed, groundFriction); // Ground accelerate + + if (Input.GetButton("Jump")) + { + velocity.y = jumpPower; // Jump + } + } } else { - moveDirection.y = movementDirectionY; + velocity.y -= gravity * Time.deltaTime; // Apply gravity + + if (canMove) + { + // Air acceleration is now dependant on how aligned the move direction is with the current velocity direction + // The closer to straight ahead, the more acceleration, allowing for more maneuverability while keeping strafe speed under control + float currentSpeed = Vector3.Dot(velocity, wishDir); + float accel = airAcceleration * Mathf.Max(0, 1 - currentSpeed / speed); + Accelerate(wishDir, speed, accel); + } } - if (!characterController.isGrounded) - { - moveDirection.y -= gravity * Time.deltaTime; - } - else if (!Input.GetButton("Jump")) - { - bhopMultiplier = 1f; - isJumping = false; - } - - characterController.Move(moveDirection * Time.deltaTime); + // Apply movement + characterController.Move(velocity * Time.deltaTime); + // Looking if (canMove) { rotationX += -Input.GetAxis("Mouse Y") * lookSpeed; @@ -74,4 +85,18 @@ public class FPSController : MonoBehaviour transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0); } } + + // Acceleration function to add force to the velocity in the given direction + void Accelerate(Vector3 wishDir, float wishSpeed, float accel) + { + float currentSpeed = Vector3.Dot(velocity, wishDir); + float addSpeed = wishSpeed - currentSpeed; + if (addSpeed <= 0) return; + float accelerationSpeed = accel * Time.deltaTime * wishSpeed; + if (accelerationSpeed > addSpeed) + { + accelerationSpeed = addSpeed; + } + velocity += wishDir.normalized * accelerationSpeed; + } }