diff --git a/Assets/Scripts/Player/PlayerFixedUpdate.cs b/Assets/Scripts/Player/PlayerFixedUpdate.cs new file mode 100644 index 0000000..b4c3902 --- /dev/null +++ b/Assets/Scripts/Player/PlayerFixedUpdate.cs @@ -0,0 +1,82 @@ +using UnityEngine; + +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) + { + // Checks wether the player is grounded + // Can be overriden by passing true to force a jump + if (m_Grounded || force) + { + // Applies an upwards force simulating a jump + m_Body.AddForce(transform.up * m_JumpForce * m_Body.mass, ForceMode.Impulse); + } + } + + // Fixed Update is called once per physics update + private void FixedUpdate() + { + // Works out the new state of the player + UpdatePlayerState(); + + // Calculates the movement direction + m_MoveDir = (m_Orientation.forward * m_Input.y) + (m_Orientation.right * m_Input.x); + + // + if (m_OnSlope) + { + // Calculates better move direction for sliding + m_MoveDir = Vector3.ProjectOnPlane(m_MoveDir, m_SlopeHit.normal).normalized; + } + + // Runs correct update function depending on player state + switch (m_State) + { + 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); + + // Stops player sliding slopes when they don't want to + if (m_OnSlope) + { m_Body.useGravity = false; } + + // Non-Slope running requires gravity on + else + { m_Body.useGravity = true; } + + break; + + case PlayerState.SLIDING: + m_Body.useGravity = false; // Disables gravity on slopes + + // Calls correct update function + UpdateSlidingState(); + break; + + case PlayerState.WALL_RUNNING: + // Calls correct update function + UpdateWallRunState(); + m_Body.useGravity = false; // Disables gravity on walls to stop the player sliding off them + 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); + + // Remvoes tiny ammounts of velocity because it was annoying me + Vector3 v = m_Body.velocity; + if (Mathf.Abs(v.x) < 0.1f) { v.x = 0.0f; } + if (Mathf.Abs(v.y) < 0.1f) { v.y = 0.0f; } + if (Mathf.Abs(v.z) < 0.1f) { v.z = 0.0f; } + m_Body.velocity = v; + } +} diff --git a/Assets/Scripts/Player/PlayerFixedUpdate.cs.meta b/Assets/Scripts/Player/PlayerFixedUpdate.cs.meta new file mode 100644 index 0000000..c694643 --- /dev/null +++ b/Assets/Scripts/Player/PlayerFixedUpdate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9fae419f7c5aef34295d583d647bf578 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/PlayerMovement.cs b/Assets/Scripts/Player/PlayerMovement.cs index be405fb..ee57a23 100644 --- a/Assets/Scripts/Player/PlayerMovement.cs +++ b/Assets/Scripts/Player/PlayerMovement.cs @@ -1,14 +1,7 @@ using UnityEngine; using UnityEngine.UI; -public enum PlayerState -{ - RUNNING, - SLIDING, - WALL_RUNNING -} - -public class PlayerMovement : MonoBehaviour +public partial class PlayerMovement : MonoBehaviour { [Header("VIEWABLE ONLY")] [SerializeField] PlayerState m_State = PlayerState.RUNNING; @@ -85,247 +78,4 @@ public class PlayerMovement : MonoBehaviour // Stops the rigidbody from rotatating when we don't want it to m_Body.freezeRotation = true; } - - // Updates the state of the user input - private void UpdateInput() - { - // Calls get axis raw to ignore any uneeded scaling - m_Input.x = Input.GetAxisRaw("Horizontal"); - m_Input.y = Input.GetAxisRaw("Vertical"); - - // Checks wether the jump button has been pressed - m_JumpKeyPressed = Input.GetKey(m_JumpKey); - - // Checks wehter the slide key is being pressed - m_SlidingKeyPressed = Input.GetKey(m_SlideKey); - } - - // Applies drag to the player - private void ApplyDrag() - { - switch (m_State) - { - case PlayerState.SLIDING: - m_Body.drag = m_SlideDrag; - break; - - default: - // Applies different drag depending on if the player is on the ground or not - if (m_Grounded) - { m_Body.drag = m_GroundDrag; } - else - { m_Body.drag = m_AirDrag; } - - break; - } - } - - // Update is called once per frame - private void Update() - { - // Performs raycasts to see what the player is standing on - m_Grounded = Physics.Raycast(transform.position, Vector3.down, out m_GroundHit, m_PlayerHeight * 0.5f + 0.3f, m_GroundMask); - m_OnSlope = Physics.Raycast(transform.position, Vector3.down, out m_SlopeHit, m_PlayerHeight * 0.5f + 0.3f, m_SlopeMask); - - // Checks for walls either side of the player - m_HitLhsWall = Physics.Raycast(transform.position, m_Orientation.right, out m_LhsWall, m_WallCheckDistance, m_GroundMask); - m_HitRhsWall = Physics.Raycast(transform.position, -m_Orientation.right, out m_RhsWall, m_WallCheckDistance, m_GroundMask); - - // Checks the player is far enough of the ground to start wall running - m_IsFarEnoughOffGroundToWallRide = m_GroundHit.distance > m_DistanceOfFloorToWallRide; - - // Updates the state of the user input - UpdateInput(); - - // Applies drag to the player - ApplyDrag(); - - // Displays the speed of the player to the screen - m_SpeedDisplay.text = "Speed: " + m_Body.velocity.magnitude.ToString("0.00"); - } - - // Handles the logic for starting to slide - private void StartSlide() - { - // Shrinks the player to give appearance of sliding - 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); - - // Applies a boost of a force at the beginning of a slide - m_TicksOfSlideBoostLeft = 10; - } - - // Handles the logic for ending the slide - private void StopSlide() - { - // Grows the player back to normal scale - m_PlayerTransform.localScale = Vector3.one; - - // Removes any of the slide boost that may be left - m_TicksOfSlideBoostLeft = 0; - } - - // Function to manage the sliding of the player - private void UpdateSlidingState() - { - // Correctly applies force on slopes - if (m_OnSlope) - { - Vector3 slopeDir = m_SlopeHit.normal; - slopeDir.y = 0.0f - slopeDir.y; - m_Body.AddForce(slopeDir.normalized * m_SlideSpeed * m_Body.mass * 10, ForceMode.Force); - } - - // 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); - } - - //m_Body.AddForce(Vector3.down * m_Body.mass * 5.0f, ForceMode.Impulse); - } - - private void UpdateWallRunState() - { - // Calculates the foward direction of the wall - Vector3 normal = m_HitRhsWall ? m_RhsWall.normal : m_LhsWall.normal; - Vector3 foward = Vector3.Cross(normal, transform.up); - - // Flips the foward direction if facing the other direction - if ((m_Orientation.forward - foward).magnitude > (m_Orientation.forward - (-foward)).magnitude) - { foward = -foward; } - - // Applies the wall running force to the player - m_Body.AddForce(foward * m_WallRunSpeed * m_Body.mass * 10.0f, ForceMode.Force); - - // Removes any vertical velocity the player may have - m_Body.velocity = new Vector3(m_Body.velocity.x, 0.0f, m_Body.velocity.z); - } - - // 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) - { - // Checks wether the player is grounded - // Can be overriden by passing true to force a jump - if (m_Grounded || force) - { - // Applies an upwards force simulating a jump - m_Body.AddForce(transform.up * m_JumpForce * m_Body.mass, ForceMode.Impulse); - } - } - - private void UpdatePlayerState() - { - // Stores previous state - PlayerState previous = m_State; - - // Works out wether the player's velocity is high enough to slide - Vector3 vel = m_Body.velocity; - bool canSlide = !(Mathf.Abs(vel.x) < m_SlideRequiredSpeed && Mathf.Abs(vel.z) < m_SlideRequiredSpeed); - - // Checks if the player is in the wall running state - if (m_HitLhsWall || m_HitRhsWall) - { m_State = PlayerState.WALL_RUNNING; } - - // Checks if the player is in the wall riding state - else if (m_SlidingKeyPressed && (canSlide || m_OnSlope) && m_Grounded) - { m_State = PlayerState.SLIDING; } - - // Defaults to ruuning - else { m_State = PlayerState.RUNNING; } - - // Exits early if the state has not changed - if (previous == m_State) - { return; } - - // Calls exit function of old state - switch (previous) - { - case PlayerState.SLIDING: - StopSlide(); - break; - - default: - break; - } - - // Calls entry function of new state - switch (m_State) - { - case PlayerState.SLIDING: - StartSlide(); - break; - - default: - break; - } - } - - // Fixed Update is called once per physics update - private void FixedUpdate() - { - // Works out the new state of the player - UpdatePlayerState(); - - // Calculates the movement direction - m_MoveDir = (m_Orientation.forward * m_Input.y) + (m_Orientation.right * m_Input.x); - - // - if (m_OnSlope) - { - // Calculates better move direction for sliding - m_MoveDir = Vector3.ProjectOnPlane(m_MoveDir, m_SlopeHit.normal).normalized; - } - - // Runs correct update function depending on player state - switch (m_State) - { - 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); - - // Stops player sliding slopes when they don't want to - if (m_OnSlope) - { m_Body.useGravity = false; } - - // Non-Slope running requires gravity on - else - { m_Body.useGravity = true; } - - break; - - case PlayerState.SLIDING: - m_Body.useGravity = false; // Disables gravity on slopes - - // Calls correct update function - UpdateSlidingState(); - break; - - case PlayerState.WALL_RUNNING: - // Calls correct update function - UpdateWallRunState(); - m_Body.useGravity = false; // Disables gravity on walls to stop the player sliding off them - 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); - - // Remvoes tiny ammounts of velocity because it was annoying me - Vector3 v = m_Body.velocity; - if (Mathf.Abs(v.x) < 0.1f) { v.x = 0.0f; } - if (Mathf.Abs(v.y) < 0.1f) { v.y = 0.0f; } - if (Mathf.Abs(v.z) < 0.1f) { v.z = 0.0f; } - m_Body.velocity = v; - } } diff --git a/Assets/Scripts/Player/PlayerState.cs b/Assets/Scripts/Player/PlayerState.cs new file mode 100644 index 0000000..b6960e7 --- /dev/null +++ b/Assets/Scripts/Player/PlayerState.cs @@ -0,0 +1,58 @@ +using UnityEngine; + +public enum PlayerState +{ + RUNNING, + SLIDING, + WALL_RUNNING +} + +public partial class PlayerMovement : MonoBehaviour +{ + private void UpdatePlayerState() + { + // Stores previous state + PlayerState previous = m_State; + + // Works out wether the player's velocity is high enough to slide + Vector3 vel = m_Body.velocity; + bool canSlide = !(Mathf.Abs(vel.x) < m_SlideRequiredSpeed && Mathf.Abs(vel.z) < m_SlideRequiredSpeed); + + // Checks if the player is in the wall running state + if (m_HitLhsWall || m_HitRhsWall) + { m_State = PlayerState.WALL_RUNNING; } + + // Checks if the player is in the wall riding state + else if (m_SlidingKeyPressed && (canSlide || m_OnSlope) && m_Grounded) + { m_State = PlayerState.SLIDING; } + + // Defaults to ruuning + else { m_State = PlayerState.RUNNING; } + + // Exits early if the state has not changed + if (previous == m_State) + { return; } + + // Calls exit function of old state + switch (previous) + { + case PlayerState.SLIDING: + StopSlide(); + break; + + default: + break; + } + + // Calls entry function of new state + switch (m_State) + { + case PlayerState.SLIDING: + StartSlide(); + break; + + default: + break; + } + } +} diff --git a/Assets/Scripts/Player/PlayerState.cs.meta b/Assets/Scripts/Player/PlayerState.cs.meta new file mode 100644 index 0000000..4e987a5 --- /dev/null +++ b/Assets/Scripts/Player/PlayerState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1ae0415a51315a345a23c758f9559c4f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/PlayerUpdate.cs b/Assets/Scripts/Player/PlayerUpdate.cs new file mode 100644 index 0000000..2b97630 --- /dev/null +++ b/Assets/Scripts/Player/PlayerUpdate.cs @@ -0,0 +1,62 @@ +using UnityEngine; + +public partial class PlayerMovement : MonoBehaviour +{ + // Updates the state of the user input + private void UpdateInput() + { + // Calls get axis raw to ignore any uneeded scaling + m_Input.x = Input.GetAxisRaw("Horizontal"); + m_Input.y = Input.GetAxisRaw("Vertical"); + + // Checks wether the jump button has been pressed + m_JumpKeyPressed = Input.GetKey(m_JumpKey); + + // Checks wehter the slide key is being pressed + m_SlidingKeyPressed = Input.GetKey(m_SlideKey); + } + + // Applies drag to the player + private void ApplyDrag() + { + switch (m_State) + { + case PlayerState.SLIDING: + m_Body.drag = m_SlideDrag; + break; + + default: + // Applies different drag depending on if the player is on the ground or not + if (m_Grounded) + { m_Body.drag = m_GroundDrag; } + else + { m_Body.drag = m_AirDrag; } + + break; + } + } + + // Update is called once per frame + private void Update() + { + // Performs raycasts to see what the player is standing on + m_Grounded = Physics.Raycast(transform.position, Vector3.down, out m_GroundHit, m_PlayerHeight * 0.5f + 0.3f, m_GroundMask); + m_OnSlope = Physics.Raycast(transform.position, Vector3.down, out m_SlopeHit, m_PlayerHeight * 0.5f + 0.3f, m_SlopeMask); + + // Checks for walls either side of the player + m_HitLhsWall = Physics.Raycast(transform.position, m_Orientation.right, out m_LhsWall, m_WallCheckDistance, m_GroundMask); + m_HitRhsWall = Physics.Raycast(transform.position, -m_Orientation.right, out m_RhsWall, m_WallCheckDistance, m_GroundMask); + + // Checks the player is far enough of the ground to start wall running + m_IsFarEnoughOffGroundToWallRide = m_GroundHit.distance > m_DistanceOfFloorToWallRide; + + // Updates the state of the user input + UpdateInput(); + + // Applies drag to the player + ApplyDrag(); + + // Displays the speed of the player to the screen + m_SpeedDisplay.text = "Speed: " + m_Body.velocity.magnitude.ToString("0.00"); + } +} diff --git a/Assets/Scripts/Player/PlayerUpdate.cs.meta b/Assets/Scripts/Player/PlayerUpdate.cs.meta new file mode 100644 index 0000000..703cd58 --- /dev/null +++ b/Assets/Scripts/Player/PlayerUpdate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ee19221d979b1404e936704d01b67395 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/SlidingMovement.cs b/Assets/Scripts/Player/SlidingMovement.cs new file mode 100644 index 0000000..db97d51 --- /dev/null +++ b/Assets/Scripts/Player/SlidingMovement.cs @@ -0,0 +1,47 @@ +using UnityEngine; + +public partial class PlayerMovement : MonoBehaviour +{ + // Handles the logic for starting to slide + private void StartSlide() + { + // Shrinks the player to give appearance of sliding + 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); + + // Applies a boost of a force at the beginning of a slide + m_TicksOfSlideBoostLeft = 10; + } + + // Handles the logic for ending the slide + private void StopSlide() + { + // Grows the player back to normal scale + m_PlayerTransform.localScale = Vector3.one; + + // Removes any of the slide boost that may be left + m_TicksOfSlideBoostLeft = 0; + } + + // Function to manage the sliding of the player + private void UpdateSlidingState() + { + // Correctly applies force on slopes + if (m_OnSlope) + { + Vector3 slopeDir = m_SlopeHit.normal; + slopeDir.y = 0.0f - slopeDir.y; + m_Body.AddForce(slopeDir.normalized * m_SlideSpeed * m_Body.mass * 10, ForceMode.Force); + } + + // 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); + } + + //m_Body.AddForce(Vector3.down * m_Body.mass * 5.0f, ForceMode.Impulse); + } +} diff --git a/Assets/Scripts/Player/SlidingMovement.cs.meta b/Assets/Scripts/Player/SlidingMovement.cs.meta new file mode 100644 index 0000000..4ef69cc --- /dev/null +++ b/Assets/Scripts/Player/SlidingMovement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ed75d9f478d44624588ca6560acda073 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/WallRunningMovement.cs b/Assets/Scripts/Player/WallRunningMovement.cs new file mode 100644 index 0000000..210511c --- /dev/null +++ b/Assets/Scripts/Player/WallRunningMovement.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +public partial class PlayerMovement : MonoBehaviour +{ + private void UpdateWallRunState() + { + // Calculates the foward direction of the wall + Vector3 normal = m_HitRhsWall ? m_RhsWall.normal : m_LhsWall.normal; + Vector3 foward = Vector3.Cross(normal, transform.up); + + // Flips the foward direction if facing the other direction + if ((m_Orientation.forward - foward).magnitude > (m_Orientation.forward - (-foward)).magnitude) + { foward = -foward; } + + // Applies the wall running force to the player + m_Body.AddForce(foward * m_WallRunSpeed * m_Body.mass * 10.0f, ForceMode.Force); + + // Removes any vertical velocity the player may have + m_Body.velocity = new Vector3(m_Body.velocity.x, 0.0f, m_Body.velocity.z); + } +} diff --git a/Assets/Scripts/Player/WallRunningMovement.cs.meta b/Assets/Scripts/Player/WallRunningMovement.cs.meta new file mode 100644 index 0000000..731f5ba --- /dev/null +++ b/Assets/Scripts/Player/WallRunningMovement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 77c79bab67f630244b74f26991f73c7f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: