add: dashing

remove: sprinting and crouching
This commit is contained in:
KOSMOGOR
2025-04-30 12:28:06 +03:00
parent d8da83d8f2
commit 68cf00fd21
2 changed files with 52 additions and 28 deletions
+7 -5
View File
@@ -986,15 +986,17 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ecc089d81c2c4574693f263b2365fdc5, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 5
sprintSpeedMultiplier: 2
crouchSpeedMultiplier: 0.5
crouchHeightMultiplier: 0.5
speed: 8
dashVelocity: 15
dashTime: 0.3
jumpForce: 10
gravityMultiplier: 1
isCrouching: 0
gravityVelocity: {x: 0, y: 0, z: 0}
grounded: 0
movementInactive: 0
gravityInactive: 0
constantVelocity: {x: 0, y: 0, z: 0}
constantVelocityActive: 0
--- !u!114 &1198108720433238150
MonoBehaviour:
m_ObjectHideFlags: 0
+45 -23
View File
@@ -3,18 +3,20 @@ using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Horizontal movement")]
public float speed = 1f;
public float sprintSpeedMultiplier = 2f;
public float crouchSpeedMultiplier = 0.5f;
public float crouchHeightMultiplier = 0.5f;
public float speed = 5f;
public float dashVelocity = 10f;
public float dashTime = 0.5f;
[Header("Vertical movement")]
public float jumpForce = 1f;
public float gravityMultiplier = 1f;
[Header("Debug info")]
[SerializeField] bool isCrouching = false;
[SerializeField] Vector3 gravityVelocity;
[SerializeField] bool grounded = false;
[SerializeField] float movementInactive = 0;
[SerializeField] float gravityInactive = 0;
[SerializeField] Vector3 constantVelocity = Vector3.zero;
[SerializeField] float constantVelocityActive = 0;
CharacterController characterController;
GroundDetector ground;
@@ -26,48 +28,68 @@ public class PlayerMovement : MonoBehaviour
void Update() {
grounded = ground.onGround;
HandleCrouching();
HandleMovement();
HandleJumping();
HandleMovement();
HandleDashing();
DoGravity();
}
void HandleCrouching() {
if (Input.GetKeyDown(KeyCode.C)) {
isCrouching = !isCrouching;
if (isCrouching) {
characterController.center += Vector3.down * (characterController.height * (1 - crouchHeightMultiplier)) / 2;
characterController.height *= crouchHeightMultiplier;
} else {
characterController.height /= crouchHeightMultiplier;
characterController.center -= Vector3.down * (characterController.height * (1 - crouchHeightMultiplier)) / 2;
}
}
HandleConstantVelocity();
}
void HandleJumping() {
if (movementInactive > 0) return;
if (Input.GetKey(KeyCode.Space) && grounded) {
gravityVelocity = Vector3.up * jumpForce;
}
}
void HandleMovement() {
if (movementInactive > 0) {
movementInactive = Mathf.Max(movementInactive -= Time.deltaTime, 0);
return;
}
float horInput = Input.GetAxis("Horizontal");
float verInput = Input.GetAxis("Vertical");
Vector3 movementVec = new(horInput, 0, verInput);
movementVec = transform.TransformDirection(movementVec);
movementVec = Vector3.ClampMagnitude(movementVec * speed, speed) * Time.deltaTime;
float modifier = isCrouching ? crouchSpeedMultiplier : Input.GetKey(KeyCode.LeftShift) ? sprintSpeedMultiplier : 1;
movementVec = Vector3.ClampMagnitude(movementVec * speed, speed);
characterController.Move(movementVec * Time.deltaTime);
}
characterController.Move(movementVec * modifier);
void HandleDashing() {
if (Input.GetKeyDown(KeyCode.LeftShift) && constantVelocityActive == 0) {
float horInput = Input.GetAxis("Horizontal");
float verInput = Input.GetAxis("Vertical");
Vector3 movementVec = new(horInput, 0, verInput);
if (movementVec.magnitude < 0.1f) return;
constantVelocity = transform.TransformDirection(movementVec.normalized * dashVelocity);
constantVelocityActive = dashTime;
SetInactive(dashTime, dashTime);
}
}
void DoGravity() {
if (gravityInactive > 0) {
gravityVelocity = Vector3.zero;
gravityInactive = Mathf.Max(gravityInactive -= Time.deltaTime, 0);
return;
}
if (grounded && gravityVelocity.y < 0) gravityVelocity = Vector3.zero;
gravityVelocity += Physics.gravity * (Time.deltaTime * gravityMultiplier);
characterController.Move(gravityVelocity * Time.deltaTime);
}
void HandleConstantVelocity() {
if (constantVelocityActive > 0) {
characterController.Move(constantVelocity * Time.deltaTime);
constantVelocityActive = Mathf.Max(constantVelocityActive - Time.deltaTime, 0);
}
}
void SetInactive(float movement, float gravity) {
movementInactive = movement;
gravityInactive = gravity;
}
void OnControllerColliderHit(ControllerColliderHit hit) {
hit.gameObject.SendMessage("ReverseOnControllerColliderHit", hit, SendMessageOptions.DontRequireReceiver);
}