Now able to look any direction during wall ride

Also made a key bind for wallride
This commit is contained in:
2025-03-28 13:55:49 +00:00
parent b69a6028f6
commit 639cb88e63
8 changed files with 210 additions and 132 deletions

View File

@@ -0,0 +1,9 @@
using UnityEngine;
public partial class PlayerMovement : MonoBehaviour
{
private void OnTriggerStay(Collider other)
{
m_WallCollisions.Add(other);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8782e0f625dd3a74a9e1fd75478a6688
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -78,5 +78,8 @@ public partial class PlayerMovement : MonoBehaviour
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;
// Clears all stored collisions
m_WallCollisions.Clear();
}
}

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
@@ -33,6 +34,7 @@ public partial class PlayerMovement : MonoBehaviour
[Header("KeyBinds")]
[SerializeField] KeyCode m_JumpKey;
[SerializeField] KeyCode m_SlideKey;
[SerializeField] KeyCode m_WallRunKey;
[Header("References")]
[SerializeField] Rigidbody m_Body;
@@ -48,6 +50,7 @@ public partial class PlayerMovement : MonoBehaviour
// Key state trackers
bool m_JumpKeyPressed = false;
bool m_SlidingKeyPressed = false;
bool m_WallRunKeyPressed = false;
// The direction to move the player
Vector3 m_MoveDir;
@@ -56,10 +59,6 @@ public partial class PlayerMovement : MonoBehaviour
bool m_Grounded = false;
bool m_OnSlope = false;
// Trackers for the walls
bool m_HitLhsWall = false;
bool m_HitRhsWall = false;
// Tracks if the distance of the ground is big enough
bool m_IsFarEnoughOffGroundToWallRide = false;
@@ -69,13 +68,29 @@ public partial class PlayerMovement : MonoBehaviour
// Raycast hit objects
RaycastHit m_GroundHit;
RaycastHit m_SlopeHit;
RaycastHit m_LhsWall;
RaycastHit m_RhsWall;
//
BoxCollider m_WallCollider;
//
List<Collider> m_WallCollisions;
//
Vector3 m_WallNormal;
// Start is called before the first frame update
private void Start()
{
// Stops the rigidbody from rotatating when we don't want it to
m_Body.freezeRotation = true;
// Creates the wall collider
m_WallCollider = gameObject.AddComponent<BoxCollider>();
m_WallCollider.size = new Vector3(m_WallCheckDistance * 2, 0.2f, m_WallCheckDistance * 2);
m_WallCollider.providesContacts = true;
m_WallCollider.isTrigger = true;
// Allocates memory for the list of collisions
m_WallCollisions = new List<Collider>();
}
}

View File

@@ -19,7 +19,7 @@ public partial class PlayerMovement : MonoBehaviour
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)
if (GetNormalOfClosestCollider(out m_WallNormal) && m_WallRunKeyPressed)
{ m_State = PlayerState.WALL_RUNNING; }
// Checks if the player is in the wall riding state

View File

@@ -9,11 +9,10 @@ public partial class PlayerMovement : MonoBehaviour
m_Input.x = Input.GetAxisRaw("Horizontal");
m_Input.y = Input.GetAxisRaw("Vertical");
// Checks wether the jump button has been pressed
// Updates key press states
m_JumpKeyPressed = Input.GetKey(m_JumpKey);
// Checks wehter the slide key is being pressed
m_SlidingKeyPressed = Input.GetKey(m_SlideKey);
m_WallRunKeyPressed = Input.GetKey(m_WallRunKey);
}
// Applies drag to the player
@@ -43,10 +42,6 @@ public partial class PlayerMovement : MonoBehaviour
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;

View File

@@ -2,11 +2,56 @@ using UnityEngine;
public partial class PlayerMovement : MonoBehaviour
{
bool GetNormalOfClosestCollider(out Vector3 normal)
{
Debug.Log(m_WallCollisions.Count);
float dist = Mathf.Infinity;
Collider closest = null;
foreach (Collider collision in m_WallCollisions)
{
Vector3 pos = collision.ClosestPoint(transform.position);
Vector3 dif = transform.position - pos;
float distance = dif.magnitude;
dist = Mathf.Min(dist, distance);
if (dist == distance)
{
closest = collision;
}
}
if (dist > m_WallCheckDistance)
{
normal = Vector3.zero;
return false;
}
Vector3 point = closest.ClosestPoint(transform.position);
Vector3 dir = point - transform.position;
RaycastHit hit;
if (Physics.Raycast(transform.position, dir.normalized, out hit, (m_WallCheckDistance * 2.0f) + 1.0f, m_GroundMask))
{
normal = hit.normal;
return true;
}
else
{
Debug.LogError("SOMETHING WENT WRONG");
normal = Vector3.zero;
return false;
}
}
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);
Vector3 foward = Vector3.Cross(m_WallNormal, transform.up);
// Flips the foward direction if facing the other direction
if ((m_Orientation.forward - foward).magnitude > (m_Orientation.forward - (-foward)).magnitude)