feat: Wall run

This commit is contained in:
2026-06-08 01:09:28 +02:00
parent 6a26357204
commit 5f6edb910f
113 changed files with 68759 additions and 37 deletions

View File

@@ -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)
{

View File

@@ -138,6 +138,8 @@ public class PlayerAnimation : MonoBehaviour
case Player.PlayerState.Idle:
case Player.PlayerState.Moving:
_references.Anim.SetFloat("move", Player.Instance.State.HorizontalVelocity.magnitude);
_references.Anim.SetFloat("run", Mathf.InverseLerp(0, .8f, Player.Instance.State.Running));
_references.Anim.SetFloat("wall", Player.Instance.State.AnimationCenter * .5f + .5f);
break;
}
}

View File

@@ -0,0 +1,126 @@
using UnityEngine;
using UnityEngine.InputSystem;
using static Player;
public class PlayerRockerLauncher : MonoBehaviour
{
[System.Serializable]
public class References
{
public InputActionAsset InputActions;
public GameObject RocketPrefab;
public GameObject LauncherMesh;
public Transform HatchJoint;
public Transform RootJoint;
public Transform YawJoint;
public Transform PitchJoint;
public Transform RocketOrigin;
}
[System.Serializable]
public class Settings
{
public bool InfiniteAmo;
}
[System.Serializable]
public class StateContainer
{
public int RocketCount;
}
[SerializeField] private Settings _settings;
[SerializeField] private References _references;
[SerializeField, ReadOnly] private StateContainer _state;
private InputAction _targetAction;
private InputAction _launchAction;
private Camera _camera;
private Rocket _rocket;
private Vector3 _rootPos;
private float _targeting;
void Awake()
{
//if (!Instance)
// Instance = this;
_rootPos = _references.RootJoint.transform.localPosition;
// Inputs
_targetAction = _references.InputActions.FindActionMap("Player").FindAction("Target");
_launchAction = _references.InputActions.FindActionMap("Player").FindAction("Attack");
// Camera
_camera = Camera.main;
}
void OnEnable()
{
_targetAction?.Enable();
_launchAction?.Enable();
SetLauncher();
}
void OnDisable()
{
_targetAction?.Disable();
_launchAction?.Disable();
}
void Update()
{
float t = Time.deltaTime;
HandleTargeting(t);
SetLauncher();
HandleLaunch();
ManageRockets();
}
private void HandleTargeting(float deltaTime)
{
bool executeTarget = _targetAction.IsPressed() && _rocket;
_targeting = Mathf.Clamp01(_targeting + deltaTime * 2 * (executeTarget ? 1 : -1));
}
private void SetLauncher()
{
_references.LauncherMesh.SetActive(_targeting > 0);
_references.RocketOrigin.gameObject.SetActive(_targeting > 0);
_references.HatchJoint.localRotation = Quaternion.Slerp(Quaternion.Euler(0, -45, 0), Quaternion.Euler(-75, -45, 0), _targeting);
_references.RootJoint.localPosition = _rootPos + Vector3.up * Mathf.Lerp(-.8f, 0, _targeting);
Quaternion yaw = Quaternion.LookRotation(Vector3.ProjectOnPlane(_camera.transform.forward, Vector3.up));
yaw = Quaternion.Slerp(_references.RootJoint.rotation, yaw, _targeting);
Quaternion pitch = Quaternion.Slerp(Quaternion.Euler(0, 0, 0), Quaternion.Euler(_camera.transform.eulerAngles.x + 45, 0, 0), Mathf.InverseLerp(.6f, 1, _targeting));
_references.YawJoint.rotation = yaw;
_references.PitchJoint.localRotation = pitch;
}
private void HandleLaunch()
{
if (_rocket && _targeting == 1 && _launchAction.triggered)
{
_rocket.Launch();
_rocket = null;
}
}
private void ManageRockets()
{
if ((_state.RocketCount > 0 || _settings.InfiniteAmo) && !_rocket && _targeting == 0)
{
GameObject obj = Instantiate(_references.RocketPrefab, _references.RocketOrigin);
obj.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.Euler(0, 0, 0));
obj.transform.localScale = Vector3.one;
_rocket = obj.GetComponent<Rocket>();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 49dc81798e7cf0e49a7711d9fbe04cfb

View File

@@ -6,6 +6,7 @@ public class Rocket : MonoBehaviour
[System.Serializable]
public class References
{
public Collider Collider;
public Rigidbody Rigidbody;
public ParticleSystem Fire;
public GameObject Explosion;
@@ -14,9 +15,11 @@ public class Rocket : MonoBehaviour
[System.Serializable]
public class Settings
{
public bool IsKinematic = false;
public bool LaunchOnEnable = false;
public float Speed = 20;
public float Duration = 5;
public float GostTime = 0;
}
[System.Serializable]
@@ -30,13 +33,18 @@ public class Rocket : MonoBehaviour
[SerializeField] private References _references;
[SerializeField] private Settings _settings;
[SerializeField, ReadOnly] private State _state;
private Vector3 _lastPos;
void OnEnable()
{
_references.Rigidbody.isKinematic = _settings.IsKinematic;
if (_settings.GostTime > 0)
_references.Collider.enabled = false;
if (_settings.LaunchOnEnable)
{
Launch();
}
}
void Update()
@@ -45,6 +53,7 @@ public class Rocket : MonoBehaviour
{
Vector3 force = transform.up * _settings.Speed * Time.deltaTime * 100;
_references.Collider.enabled = _state.FlightTime > _settings.GostTime;
_references.Rigidbody.AddForce(force);
_state.FlightTime += Time.deltaTime;
@@ -57,6 +66,27 @@ public class Rocket : MonoBehaviour
}
}
void LateUpdate()
{
if (_state.Launched && _state.FlightTime > _settings.Duration)
{
Vector3 currentPos = transform.position;
Vector3 lookDir = currentPos - _lastPos;
_lastPos = transform.position;
if (lookDir.sqrMagnitude > .001f)
{
lookDir = lookDir.normalized;
Quaternion targetRot = Quaternion.LookRotation(lookDir, Vector3.up);
targetRot *= Quaternion.Euler(90, 0, 0);
targetRot = Quaternion.Lerp(transform.rotation, targetRot, Time.deltaTime * 10);
_references.Rigidbody.MoveRotation(targetRot);
}
}
}
void OnCollisionEnter(Collision col)
{
if (_state.Launched && _state.FlightTime > .5f)
@@ -71,10 +101,16 @@ public class Rocket : MonoBehaviour
[ContextMenu("Launch")]
public void Launch()
{
transform.parent = null;
_references.Rigidbody.isKinematic = false;
_state.Launched = true;
_state.Disabled = false;
_state.FlightTime = 0;
_references.Fire.Play();
_lastPos = transform.position;
}
}

View File

@@ -0,0 +1,10 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public void LoadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 98ebfc824c0127543899cdebb2b175f3