From 4f47cd883a554f535cb23a66598e9718eb267ed3 Mon Sep 17 00:00:00 2001 From: Pasha Bibko Date: Wed, 2 Apr 2025 15:46:11 +0100 Subject: [PATCH] Did stuff --- Assets/Scripts/Player/PlayerFixedUpdate.cs | 25 ++++++++++---------- Assets/Scripts/Player/PlayerUpdate.cs | 2 +- Assets/Scripts/Player/SlidingMovement.cs | 17 ++++++++++--- Assets/Scripts/Player/WallRunningMovement.cs | 3 +++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Assets/Scripts/Player/PlayerFixedUpdate.cs b/Assets/Scripts/Player/PlayerFixedUpdate.cs index 93c4e0a..5c76cff 100644 --- a/Assets/Scripts/Player/PlayerFixedUpdate.cs +++ b/Assets/Scripts/Player/PlayerFixedUpdate.cs @@ -4,14 +4,18 @@ public partial class PlayerMovement : MonoBehaviour { // Function to make the player jump // The function checks wether the player is grounded so external checks are not needed - private void Jump(bool force = false) + private void Jump(float scale = 1.0f, bool force = false) { // Checks wether the player is grounded // Can be overriden by passing true to force a jump if (m_Grounded || force) { + // Removes all downwards velocity + Vector3 v = m_Body.velocity; + m_Body.velocity = new Vector3(v.x, Mathf.Max(v.y), v.z); + // Applies an upwards force simulating a jump - m_Body.AddForce(transform.up * m_JumpForce * m_Body.mass, ForceMode.Impulse); + m_Body.AddForce(Vector3.up * m_JumpForce * m_Body.mass * scale, ForceMode.Impulse); } } @@ -36,7 +40,7 @@ public partial class PlayerMovement : MonoBehaviour { case PlayerState.RUNNING: // Adds the force to the rigid body - m_Body.AddForce(m_MoveDir.normalized * m_MoveSpeed * m_Body.mass * 10.0f, ForceMode.Force); + m_Body.AddForce(m_MoveDir.normalized * m_MoveSpeed * m_Body.mass * (m_Grounded ? 10.0f : 5.0f), ForceMode.Force); // Stops player sliding slopes when they don't want to if (m_OnSlope) @@ -46,6 +50,10 @@ public partial class PlayerMovement : MonoBehaviour else { m_Body.useGravity = true; } + // Checks if the player wants to jump + // Jump function does the checking if they can + if (m_JumpKeyPressed) { Jump(); } + break; case PlayerState.SLIDING: @@ -62,13 +70,6 @@ public partial class PlayerMovement : MonoBehaviour break; } - // Calls the Jump function if the user has pressed jump - // No grounded checks needed as Jump() function does that internally - if (m_JumpKeyPressed) - { - Jump(); - } - // Updates the counter for slide boost updates left m_TicksOfSlideBoostLeft = (int)Mathf.Clamp(m_TicksOfSlideBoostLeft - 1, 0, Mathf.Infinity); @@ -80,8 +81,8 @@ public partial class PlayerMovement : MonoBehaviour m_Body.velocity = v; // Doubles gravity if falling to feel less floaty - if (v.y < 0.0f) { GravityController.Instance().SetScale(2.0f); } - else { GravityController.Instance().SetScale(1.0f); } + if (v.y < 0.0f) { GravityController.Instance().SetScale(8.0f); } + else { GravityController.Instance().SetScale(3f); } // Clears all stored collisions m_WallCollisions.Clear(); diff --git a/Assets/Scripts/Player/PlayerUpdate.cs b/Assets/Scripts/Player/PlayerUpdate.cs index ed1df16..3678e7a 100644 --- a/Assets/Scripts/Player/PlayerUpdate.cs +++ b/Assets/Scripts/Player/PlayerUpdate.cs @@ -56,6 +56,6 @@ public partial class PlayerMovement : MonoBehaviour ApplyDrag(); // Displays the speed of the player to the screen - m_SpeedDisplay.text = "Speed: " + m_Body.velocity.magnitude.ToString("0.00"); + m_SpeedDisplay.text = "Speed: " + new Vector3(m_Body.velocity.x, 0.0f, m_Body.velocity.z).magnitude.ToString("0.00") + " m/s"; } } diff --git a/Assets/Scripts/Player/SlidingMovement.cs b/Assets/Scripts/Player/SlidingMovement.cs index ca918c9..924e07a 100644 --- a/Assets/Scripts/Player/SlidingMovement.cs +++ b/Assets/Scripts/Player/SlidingMovement.cs @@ -9,7 +9,7 @@ public partial class PlayerMovement : MonoBehaviour m_PlayerTransform.localScale = new Vector3(1.0f, m_SlideScaler, 1.0f); // Applies a downward force as shrinking the player scale causes them to float - m_Body.AddForce(Vector3.down * m_Body.mass * 5.0f, ForceMode.Impulse); + m_Body.AddForce(Vector3.down * m_Body.mass * 5.0f, ForceMode.Force); // Applies a boost of a force at the beginning of a slide m_TicksOfSlideBoostLeft = 10; @@ -34,14 +34,25 @@ public partial class PlayerMovement : MonoBehaviour Vector3 slopeDir = m_StandingOn.normal; slopeDir.y = 0.0f - slopeDir.y; m_Body.AddForce(slopeDir.normalized * m_SlideSpeed * m_Body.mass * 10, ForceMode.Force); + + // Checks if the player wants to jump + if (m_JumpKeyPressed) { Jump(5.0f, true); } } // If at the start of a slide provides a boost to the player or if the player is on a slope else if (m_TicksOfSlideBoostLeft != 0) { - m_Body.AddForce(m_MoveDir.normalized * m_SlideSpeed * m_Body.mass * 10, ForceMode.Force); + // Applies the boost + m_Body.AddForce(m_MoveDir.normalized * m_SlideSpeed * m_Body.mass * 2, ForceMode.Force); + + // Checks if the player wants to jump + if (m_JumpKeyPressed) { Jump(1.0f, true); } } - //m_Body.AddForce(Vector3.down * m_Body.mass * 5.0f, ForceMode.Impulse); + else + { + // Checks if the player wants to jump + if (m_JumpKeyPressed) { Jump(1.0f, true); } + } } } diff --git a/Assets/Scripts/Player/WallRunningMovement.cs b/Assets/Scripts/Player/WallRunningMovement.cs index f219b48..d1376ea 100644 --- a/Assets/Scripts/Player/WallRunningMovement.cs +++ b/Assets/Scripts/Player/WallRunningMovement.cs @@ -74,5 +74,8 @@ public partial class PlayerMovement : MonoBehaviour // Sets the last wall normal to the current normal for later use m_LastWallNormal = m_WallNormal; + + // Checks if the player wants to jump + if (m_JumpKeyPressed) { Jump(2.0f, true); } } }