Did stuff

This commit is contained in:
2025-04-02 15:46:11 +01:00
parent 1718bd7586
commit 4f47cd883a
4 changed files with 31 additions and 16 deletions

View File

@@ -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();

View File

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

View File

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

View File

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