Refactor: Dash

Repositioned some parts of dash code into skillHandler
This commit is contained in:
2025-08-28 17:09:35 +02:00
parent 25ff65c0b3
commit dc083bfc93
3 changed files with 21 additions and 19 deletions

View File

@@ -608,6 +608,8 @@ MonoBehaviour:
sprintSpeed: 10 sprintSpeed: 10
gravity: -9.81 gravity: -9.81
rotationSpeed: 10 rotationSpeed: 10
dashSpeed: 15
dashDuration: 0.25
--- !u!143 &1781483820777133190 --- !u!143 &1781483820777133190
CharacterController: CharacterController:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -9,6 +9,9 @@ public class PlayerMovement : MonoBehaviour
public float gravity = -9.81f; public float gravity = -9.81f;
public float rotationSpeed = 10f; public float rotationSpeed = 10f;
public float dashSpeed;
public float dashDuration;
private CharacterController controller; private CharacterController controller;
private Vector3 velocity; private Vector3 velocity;
private Vector3 move; private Vector3 move;
@@ -18,7 +21,7 @@ public class PlayerMovement : MonoBehaviour
private void Awake() private void Awake()
{ {
PlayerSkills = PlayerSkillTree.Instance; PlayerSkills = PlayerSkillTree.Instance;
playerSkillHandler = new PlayerSkillHandler(); playerSkillHandler = new PlayerSkillHandler(dashSpeed, dashDuration);
} }
void Start() void Start()
@@ -30,10 +33,9 @@ public class PlayerMovement : MonoBehaviour
{ {
if (Input.GetKeyDown(KeyCode.Space)) if (Input.GetKeyDown(KeyCode.Space))
{ {
/* Ugly part here, need to rewrite later */ /* Get the input for horizontal and vertical movement */
float moveX = Input.GetAxisRaw("Horizontal"); float moveX = Input.GetAxisRaw("Horizontal");
float moveZ = Input.GetAxisRaw("Vertical"); float moveZ = Input.GetAxisRaw("Vertical");
/* End of the ugly part :P */
move = new Vector3(moveX, 0, moveZ).normalized; move = new Vector3(moveX, 0, moveZ).normalized;
StartCoroutine(playerSkillHandler.DashCoroutine(controller, move)); StartCoroutine(playerSkillHandler.DashCoroutine(controller, move));
@@ -54,15 +56,6 @@ public class PlayerMovement : MonoBehaviour
void FixedUpdate() void FixedUpdate()
{ {
if (playerSkillHandler.IsDashing())
{
Physics.IgnoreLayerCollision(6, 7, true);
}
else
{
Physics.IgnoreLayerCollision(6, 7, false);
}
HandleMovement(); HandleMovement();
ApplyGravity(); ApplyGravity();
} }

View File

@@ -4,24 +4,31 @@ using UnityEngine;
public class PlayerSkillHandler public class PlayerSkillHandler
{ {
private float dashSpeed = 15f; private float dashSpeed;
private float dashDuration = 0.2f; private float dashDuration;
private bool isDashing = false; private bool isDashing = false;
public bool IsDashing() { return isDashing; } public bool IsDashing() { return isDashing; }
public PlayerSkillHandler(float dashSpeed, float dashDuration)
{
this.dashSpeed = dashSpeed;
this.dashDuration = dashDuration;
}
public IEnumerator DashCoroutine(CharacterController controller, Vector3 direction) public IEnumerator DashCoroutine(CharacterController controller, Vector3 direction)
{ {
Physics.IgnoreLayerCollision(6, 7, true); // disable collisions between player and enemies
float startTime = Time.time; float startTime = Time.time;
isDashing = true; isDashing = true; // just for now. maybe will be removed
while (Time.time - startTime < dashDuration) while (Time.time - startTime < dashDuration)
{ {
controller.Move(direction * dashSpeed * Time.deltaTime); controller.Move(dashSpeed * Time.deltaTime * direction);
yield return null; yield return null; // wait one frame
} }
isDashing = false; isDashing = false; // just for now. maybe will be removed
Physics.IgnoreLayerCollision(6, 7, false); // enable collisions between player and enemies
} }
} }