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_Script: {fileID: 11500000, guid: ecc089d81c2c4574693f263b2365fdc5, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
speed: 5 speed: 8
sprintSpeedMultiplier: 2 dashVelocity: 15
crouchSpeedMultiplier: 0.5 dashTime: 0.3
crouchHeightMultiplier: 0.5
jumpForce: 10 jumpForce: 10
gravityMultiplier: 1 gravityMultiplier: 1
isCrouching: 0
gravityVelocity: {x: 0, y: 0, z: 0} gravityVelocity: {x: 0, y: 0, z: 0}
grounded: 0 grounded: 0
movementInactive: 0
gravityInactive: 0
constantVelocity: {x: 0, y: 0, z: 0}
constantVelocityActive: 0
--- !u!114 &1198108720433238150 --- !u!114 &1198108720433238150
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
+45 -23
View File
@@ -3,18 +3,20 @@ using UnityEngine;
public class PlayerMovement : MonoBehaviour public class PlayerMovement : MonoBehaviour
{ {
[Header("Horizontal movement")] [Header("Horizontal movement")]
public float speed = 1f; public float speed = 5f;
public float sprintSpeedMultiplier = 2f; public float dashVelocity = 10f;
public float crouchSpeedMultiplier = 0.5f; public float dashTime = 0.5f;
public float crouchHeightMultiplier = 0.5f;
[Header("Vertical movement")] [Header("Vertical movement")]
public float jumpForce = 1f; public float jumpForce = 1f;
public float gravityMultiplier = 1f; public float gravityMultiplier = 1f;
[Header("Debug info")] [Header("Debug info")]
[SerializeField] bool isCrouching = false;
[SerializeField] Vector3 gravityVelocity; [SerializeField] Vector3 gravityVelocity;
[SerializeField] bool grounded = false; [SerializeField] bool grounded = false;
[SerializeField] float movementInactive = 0;
[SerializeField] float gravityInactive = 0;
[SerializeField] Vector3 constantVelocity = Vector3.zero;
[SerializeField] float constantVelocityActive = 0;
CharacterController characterController; CharacterController characterController;
GroundDetector ground; GroundDetector ground;
@@ -26,48 +28,68 @@ public class PlayerMovement : MonoBehaviour
void Update() { void Update() {
grounded = ground.onGround; grounded = ground.onGround;
HandleCrouching();
HandleMovement();
HandleJumping(); HandleJumping();
HandleMovement();
HandleDashing();
DoGravity(); DoGravity();
} HandleConstantVelocity();
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;
}
}
} }
void HandleJumping() { void HandleJumping() {
if (movementInactive > 0) return;
if (Input.GetKey(KeyCode.Space) && grounded) { if (Input.GetKey(KeyCode.Space) && grounded) {
gravityVelocity = Vector3.up * jumpForce; gravityVelocity = Vector3.up * jumpForce;
} }
} }
void HandleMovement() { void HandleMovement() {
if (movementInactive > 0) {
movementInactive = Mathf.Max(movementInactive -= Time.deltaTime, 0);
return;
}
float horInput = Input.GetAxis("Horizontal"); float horInput = Input.GetAxis("Horizontal");
float verInput = Input.GetAxis("Vertical"); float verInput = Input.GetAxis("Vertical");
Vector3 movementVec = new(horInput, 0, verInput); Vector3 movementVec = new(horInput, 0, verInput);
movementVec = transform.TransformDirection(movementVec); movementVec = transform.TransformDirection(movementVec);
movementVec = Vector3.ClampMagnitude(movementVec * speed, speed) * Time.deltaTime; movementVec = Vector3.ClampMagnitude(movementVec * speed, speed);
float modifier = isCrouching ? crouchSpeedMultiplier : Input.GetKey(KeyCode.LeftShift) ? sprintSpeedMultiplier : 1; 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() { void DoGravity() {
if (gravityInactive > 0) {
gravityVelocity = Vector3.zero;
gravityInactive = Mathf.Max(gravityInactive -= Time.deltaTime, 0);
return;
}
if (grounded && gravityVelocity.y < 0) gravityVelocity = Vector3.zero; if (grounded && gravityVelocity.y < 0) gravityVelocity = Vector3.zero;
gravityVelocity += Physics.gravity * (Time.deltaTime * gravityMultiplier); gravityVelocity += Physics.gravity * (Time.deltaTime * gravityMultiplier);
characterController.Move(gravityVelocity * Time.deltaTime); 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) { void OnControllerColliderHit(ControllerColliderHit hit) {
hit.gameObject.SendMessage("ReverseOnControllerColliderHit", hit, SendMessageOptions.DontRequireReceiver); hit.gameObject.SendMessage("ReverseOnControllerColliderHit", hit, SendMessageOptions.DontRequireReceiver);
} }