feat: Wall run
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using TMPro.EditorUtilities;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.InputSystem;
|
||||
@@ -18,13 +19,32 @@ public class Player : MonoBehaviour
|
||||
Winner,
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class WallRun
|
||||
{
|
||||
[Tooltip("Can the player run along walls?")]
|
||||
public bool CanRunOnWalls = true;
|
||||
|
||||
[Tooltip("Layers considered as walkable wall")]
|
||||
public LayerMask WallLayer = 0;
|
||||
|
||||
[Tooltip("Stick duration to wall before fall")]
|
||||
public float StickDuration = 3;
|
||||
|
||||
[Tooltip("Gravity applyed when sticked to wall")]
|
||||
public float StickedGravity = -5;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
[Header("Movements")]
|
||||
|
||||
[Tooltip("Movement speed in km/h")]
|
||||
public float Speed = 18f;
|
||||
[Tooltip("Walk speed in km/h")]
|
||||
public float WalkSpeed = 15f;
|
||||
|
||||
[Tooltip("Run speed in km/h")]
|
||||
public float RunSpeed = 30f;
|
||||
|
||||
[Tooltip("Jump force in m/s")]
|
||||
public float JumpForce = 8f;
|
||||
@@ -50,6 +70,10 @@ public class Player : MonoBehaviour
|
||||
|
||||
[Tooltip("GUI logs of current state")]
|
||||
public bool StateLogs;
|
||||
|
||||
[Header("Features")]
|
||||
|
||||
public WallRun WallRun;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
@@ -67,6 +91,21 @@ public class Player : MonoBehaviour
|
||||
|
||||
[Tooltip("Is player paused?")]
|
||||
public bool IsPaused = false;
|
||||
|
||||
[Tooltip("Is player sticked to wall?")]
|
||||
public bool IsStickedToWall;
|
||||
|
||||
[Tooltip("Can player stick to walls?")]
|
||||
public bool CanStickToWalls;
|
||||
|
||||
[Tooltip("Current running state")]
|
||||
[Range(0, 1)] public float Running;
|
||||
|
||||
[Tooltip("Animation center of gravity")]
|
||||
[Range(-1, 1)] public float AnimationCenter;
|
||||
|
||||
[Tooltip("Elapsed time since sticked to wall")]
|
||||
[Range(-1, 1)] public float StickedTime;
|
||||
|
||||
[Tooltip("Current velocity in m/s")]
|
||||
public Vector3 Velocity;
|
||||
@@ -95,6 +134,7 @@ public class Player : MonoBehaviour
|
||||
#region Private Fields
|
||||
// Inputs
|
||||
private InputAction _moveAction;
|
||||
private InputAction _runAction;
|
||||
private InputAction _jumpAction;
|
||||
|
||||
// Camera
|
||||
@@ -105,12 +145,21 @@ public class Player : MonoBehaviour
|
||||
private Vector3 _groundCheckSphereOffset;
|
||||
private float _groundCheckRadius;
|
||||
private Collider[] _overlapResults = new Collider[1];
|
||||
private Vector3 _wallDirection;
|
||||
private Vector3 _wallPosition;
|
||||
private Vector3 _lastPlatformPosition;
|
||||
private Quaternion _lastPlatformRotation;
|
||||
private Vector3 _platformVelocity;
|
||||
#endregion
|
||||
|
||||
#region Unity Lifecycle
|
||||
/*void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.matrix = transform.localToWorldMatrix;
|
||||
Gizmos.color = new Color(.2f, 1, .7f, .3f);
|
||||
Gizmos.DrawSphere(_groundCheckSphereOffset, _groundCheckRadius);
|
||||
}*/
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (!Instance)
|
||||
@@ -118,6 +167,7 @@ public class Player : MonoBehaviour
|
||||
|
||||
// Inputs
|
||||
_moveAction = _references.InputActions.FindActionMap("Player").FindAction("Move");
|
||||
_runAction = _references.InputActions.FindActionMap("Player").FindAction("Sprint");
|
||||
_jumpAction = _references.InputActions.FindActionMap("Player").FindAction("Jump");
|
||||
|
||||
// Camera
|
||||
@@ -133,12 +183,14 @@ public class Player : MonoBehaviour
|
||||
void OnEnable()
|
||||
{
|
||||
_moveAction?.Enable();
|
||||
_runAction?.Enable();
|
||||
_jumpAction?.Enable();
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
_moveAction?.Disable();
|
||||
_runAction?.Disable();
|
||||
_jumpAction?.Disable();
|
||||
}
|
||||
|
||||
@@ -159,19 +211,79 @@ public class Player : MonoBehaviour
|
||||
private void CheckGround(float deltaTime)
|
||||
{
|
||||
// Raycast for center contact
|
||||
RaycastHit rayInfo;
|
||||
Vector3 rayOrigin = transform.position + _groundCheckRayOffset;
|
||||
bool rayHit = Physics.Raycast(rayOrigin, Vector3.down, out RaycastHit rayInfo,
|
||||
_settings.GroundTolerance * 2f, _settings.GroundLayer);
|
||||
bool rayHit = Physics.Raycast(rayOrigin, Vector3.down, out rayInfo, _settings.GroundTolerance * 2f, _settings.GroundLayer);
|
||||
|
||||
// OverlapSphere for edge contact
|
||||
Vector3 sphereOrigin = transform.position + _groundCheckSphereOffset;
|
||||
int overlapCount = Physics.OverlapSphereNonAlloc(sphereOrigin, _groundCheckRadius, _overlapResults, _settings.GroundLayer);
|
||||
bool sphereHit = overlapCount > 0;
|
||||
|
||||
|
||||
bool wasGrounded = _state.IsGrounded;
|
||||
bool isGrounded = rayHit || sphereHit;
|
||||
|
||||
if (isGrounded)
|
||||
// Wall run
|
||||
bool isStickedToWall = false;
|
||||
if (!isGrounded && _settings.WallRun.CanRunOnWalls && _state.CanStickToWalls && _state.VerticalVelocity < 0)
|
||||
{
|
||||
int[] wallCheckAngles = new int[6] { 80, 70, 60, 50, 40, 30 };
|
||||
int raySign = 1;
|
||||
|
||||
for (int i = 0; i < wallCheckAngles.Length; i++)
|
||||
{
|
||||
if (isStickedToWall)
|
||||
break;
|
||||
|
||||
for (int u = -1; u <= 1; u += 2)
|
||||
{
|
||||
Vector3 rayDir = Quaternion.Euler(0, wallCheckAngles[i] * u, 0) * transform.forward;
|
||||
float rayOffset = _references.Controller.radius * .8f;
|
||||
float raySize = _references.Controller.radius * .7f;
|
||||
|
||||
isStickedToWall = Physics.Raycast(rayOrigin + rayDir * rayOffset, rayDir, out rayInfo, raySize, _settings.WallRun.WallLayer);
|
||||
Debug.DrawRay(rayOrigin + rayDir * rayOffset, rayDir * raySize, isStickedToWall ? Color.green : Color.red);
|
||||
|
||||
if (isStickedToWall)
|
||||
{
|
||||
raySign = u;
|
||||
goto AfterRaycastWall;
|
||||
}
|
||||
}
|
||||
}
|
||||
AfterRaycastWall:
|
||||
|
||||
if (!_state.IsStickedToWall && isStickedToWall)
|
||||
_state.StickedTime = 0;
|
||||
else if (_state.IsStickedToWall && !isStickedToWall)
|
||||
_state.CanStickToWalls = false;
|
||||
|
||||
_state.IsStickedToWall = isStickedToWall;
|
||||
|
||||
if (_state.IsStickedToWall)
|
||||
{
|
||||
_wallDirection = Vector3.ProjectOnPlane(Vector3.Cross(rayInfo.normal, Vector3.up), Vector3.up) * Mathf.Sign(Vector3.Cross(transform.forward, rayInfo.normal).y);
|
||||
_wallPosition = rayInfo.point;
|
||||
_state.AnimationCenter = raySign;
|
||||
_state.Running = 1;
|
||||
|
||||
if (_state.StickedTime >= _settings.WallRun.StickDuration)
|
||||
{
|
||||
_state.IsStickedToWall = false;
|
||||
_state.CanStickToWalls = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isGrounded)
|
||||
{
|
||||
_state.IsStickedToWall = false;
|
||||
_state.CanStickToWalls = true;
|
||||
}
|
||||
|
||||
if (!isStickedToWall)
|
||||
_state.AnimationCenter = 0;
|
||||
|
||||
if (isGrounded || isStickedToWall)
|
||||
{
|
||||
Transform currentGround = rayHit ? rayInfo.collider.transform : _overlapResults[0].transform;
|
||||
|
||||
@@ -227,14 +339,18 @@ public class Player : MonoBehaviour
|
||||
platformVelocity.y = _platformVelocity.y;
|
||||
_platformVelocity = platformVelocity;
|
||||
}
|
||||
|
||||
|
||||
_state.Ground = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetGravity(float deltaTime)
|
||||
{
|
||||
if (_state.IsGrounded && _state.Velocity.y < 0)
|
||||
if (_state.IsStickedToWall)
|
||||
{
|
||||
_state.Velocity.y = _settings.WallRun.StickedGravity * Mathf.Pow(_state.StickedTime / _settings.WallRun.StickDuration, 2);
|
||||
}
|
||||
else if (_state.IsGrounded && _state.Velocity.y < 0)
|
||||
{
|
||||
_state.Velocity.y = STICK_FORCE;
|
||||
}
|
||||
@@ -260,10 +376,21 @@ public class Player : MonoBehaviour
|
||||
{
|
||||
Vector2 input = _moveAction.ReadValue<Vector2>();
|
||||
|
||||
float speed = _settings.Speed * KMH_TO_MS;
|
||||
bool run = _runAction.IsPressed();
|
||||
_state.Running = Mathf.Clamp01(_state.Running + deltaTime * 4 * (run ? 1 : -1));
|
||||
|
||||
float speed = Mathf.Lerp(_settings.WalkSpeed, _settings.RunSpeed, _state.Running) * KMH_TO_MS;
|
||||
|
||||
Vector3 moveInput = new Vector3(input.x, 0, input.y); // Convert input 2D to move 3D
|
||||
moveInput = Quaternion.Euler(0, _camera.transform.eulerAngles.y, 0) * moveInput; // Rotate move toward camera
|
||||
if (_state.IsStickedToWall)
|
||||
{
|
||||
_state.StickedTime += deltaTime * Mathf.Lerp(4, 1, moveInput.magnitude);
|
||||
moveInput = _wallDirection * Mathf.Sqrt(1 - _state.StickedTime / _settings.WallRun.StickDuration);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveInput = Quaternion.Euler(0, _camera.transform.eulerAngles.y, 0) * moveInput; // Rotate move toward camera
|
||||
}
|
||||
moveInput *= speed; // Muliply move by speed
|
||||
|
||||
_state.Velocity.x = moveInput.x;
|
||||
@@ -273,6 +400,9 @@ public class Player : MonoBehaviour
|
||||
if (moveInput.sqrMagnitude > .001f)
|
||||
{
|
||||
Quaternion targetRot = Quaternion.LookRotation(moveInput);
|
||||
if (_state.IsStickedToWall)
|
||||
targetRot = Quaternion.LookRotation(_wallDirection);
|
||||
|
||||
float t = _settings.RotationSpeed * deltaTime;
|
||||
Vector3 euler = Quaternion.Slerp(transform.rotation, targetRot, t).eulerAngles;
|
||||
|
||||
@@ -282,10 +412,14 @@ public class Player : MonoBehaviour
|
||||
|
||||
private void SetJump()
|
||||
{
|
||||
if (_jumpAction.triggered && _state.IsGrounded)
|
||||
if (_jumpAction.triggered && (_state.IsGrounded || _state.IsStickedToWall))
|
||||
{
|
||||
_state.Velocity.y = _settings.JumpForce;
|
||||
_state.Ground = null;
|
||||
_state.IsStickedToWall = false;
|
||||
_state.CanStickToWalls = false;
|
||||
|
||||
_groundCheckRadius = _references.Controller.radius + _settings.GroundTolerance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,12 +427,19 @@ public class Player : MonoBehaviour
|
||||
{
|
||||
Vector3 motion = _state.Velocity + _platformVelocity;
|
||||
|
||||
if (_state.IsStickedToWall)
|
||||
{
|
||||
Vector3 wallDelta = _wallPosition - transform.position;
|
||||
wallDelta -= wallDelta.normalized * _references.Controller.radius;
|
||||
motion += wallDelta;
|
||||
}
|
||||
|
||||
_references.Controller.Move(motion * deltaTime);
|
||||
}
|
||||
|
||||
private void SetState()
|
||||
{
|
||||
if (State.IsGrounded)
|
||||
if (State.IsGrounded || State.IsStickedToWall)
|
||||
{
|
||||
if (State.HorizontalVelocity.sqrMagnitude > .1f)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user