Movement changes

This commit is contained in:
Wizzard 2023-06-20 20:13:35 -07:00
parent a849d7e20f
commit 83f3af4baf
1 changed files with 57 additions and 32 deletions

View File

@ -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;
}
}