256 lines
8.5 KiB
C#
256 lines
8.5 KiB
C#
using Code.Gameplay.Character.Controller.Input;
|
|
using Code.Gameplay.Character.Core;
|
|
using KinematicCharacterController;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Gameplay.Character.Controller.Abilities
|
|
{
|
|
public class WallRunningMechanic
|
|
{
|
|
public float MaxWallRunTime = 1.75f;
|
|
public float WallRunSpeed = 8.0f;
|
|
public float WallRunAcceleration = 25.0f;
|
|
public float WallRunGravityScale = 0.25f;
|
|
public float WallDetachImpulse = 7.0f;
|
|
public float WallContactAngleThreshold = 45.0f;
|
|
|
|
private bool _isWallRunning;
|
|
private float _wallRunTimer;
|
|
private Vector3 _wallNormal;
|
|
private Vector3 _wallTangentRight;
|
|
private Vector3 _wallTangentUp;
|
|
private Vector3 _wallContactPoint;
|
|
private Vector3 _lastMoveInputInWallSpace;
|
|
|
|
private float _surfaceAngleCos;
|
|
private bool _canWallRun;
|
|
public float WallContactHeight { get; private set; }
|
|
|
|
public Vector3 WallRunStartForward { get; private set; }
|
|
|
|
private KinematicCharacterMotor _motor;
|
|
private CharacterInputHandler _inputHandler;
|
|
|
|
public bool IsWallRunning => _isWallRunning;
|
|
|
|
public void Initialize(KinematicCharacterMotor motor, CharacterInputHandler inputHandler)
|
|
{
|
|
_motor = motor;
|
|
_inputHandler = inputHandler;
|
|
_surfaceAngleCos = Mathf.Cos(Mathf.Deg2Rad * WallContactAngleThreshold);
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
_isWallRunning = false;
|
|
_wallRunTimer = 0f;
|
|
_wallNormal = Vector3.zero;
|
|
}
|
|
|
|
public void Tick(float deltaTime)
|
|
{
|
|
if (_isWallRunning)
|
|
{
|
|
_wallRunTimer += deltaTime;
|
|
if (_wallRunTimer > MaxWallRunTime)
|
|
{
|
|
DetachFromWall(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool TryStartWallRun(Vector3 hitNormal, Vector3 hitPoint)
|
|
{
|
|
if (_isWallRunning) return false;
|
|
|
|
float dotWithUp = Vector3.Dot(hitNormal, _motor.CharacterUp);
|
|
bool isWall = Mathf.Abs(dotWithUp) < _surfaceAngleCos;
|
|
|
|
if (!isWall) return false;
|
|
|
|
if (_motor.GroundingStatus.IsStableOnGround) return false;
|
|
|
|
float velocityIntoWall = Vector3.Dot(_motor.Velocity, -hitNormal);
|
|
if (velocityIntoWall < 1f) return false;
|
|
|
|
StartWallRun(hitNormal, hitPoint);
|
|
return true;
|
|
}
|
|
|
|
private void StartWallRun(Vector3 wallNormal, Vector3 contactPoint)
|
|
{
|
|
_isWallRunning = true;
|
|
_wallRunTimer = 0f;
|
|
_wallNormal = wallNormal.normalized;
|
|
_wallContactPoint = contactPoint;
|
|
WallContactHeight = contactPoint.y;
|
|
|
|
//_wallTangentUp = Vector3.ProjectOnPlane(_motor.CharacterUp, _wallNormal).normalized;
|
|
if (_wallTangentUp.sqrMagnitude < 0.01f) _wallTangentUp = Vector3.up;
|
|
|
|
_wallTangentRight = Vector3.Cross(_wallNormal, _wallTangentUp).normalized;
|
|
|
|
_wallTangentUp = Vector3.Cross(_wallTangentRight, _wallNormal).normalized;
|
|
if (Vector3.Dot(_wallTangentUp, Vector3.up) < 0)
|
|
_wallTangentUp = -_wallTangentUp;
|
|
|
|
WallRunStartForward = _motor.CharacterForward;
|
|
_motor.ForceUnground();
|
|
}
|
|
|
|
public void HandleWallRun(ref Vector3 currentVelocity, CharacterInputs inputs, float deltaTime)
|
|
{
|
|
if (!_isWallRunning) return;
|
|
if (!IsWallStillPresent()) { DetachFromWall(false); return; }
|
|
|
|
Vector3 moveInputWorld = _inputHandler.MoveInputVector;
|
|
Vector3 moveInWallPlane = Vector3.ProjectOnPlane(moveInputWorld, _wallNormal);
|
|
|
|
Vector3 wsMove = Vector3.Project(moveInWallPlane, _wallTangentRight);
|
|
|
|
float ad = inputs.MoveAxisRight;
|
|
|
|
float sideDot = Vector3.Dot(_wallNormal, _motor.CharacterRight);
|
|
if (sideDot < 0)
|
|
ad = -ad;
|
|
|
|
Vector3 adMove = _wallTangentUp * (-ad);
|
|
|
|
Vector3 finalMove = wsMove + adMove;
|
|
finalMove = finalMove.normalized * moveInputWorld.magnitude;
|
|
|
|
Vector3 targetVelocity = finalMove * WallRunSpeed;
|
|
|
|
currentVelocity = Vector3.Lerp(
|
|
currentVelocity,
|
|
targetVelocity,
|
|
1f - Mathf.Exp(-WallRunAcceleration * deltaTime)
|
|
);
|
|
|
|
currentVelocity += Physics.gravity * (WallRunGravityScale * deltaTime);
|
|
|
|
float distanceToWall = Vector3.Dot(_motor.TransientPosition - _wallContactPoint, _wallNormal);
|
|
if (Mathf.Abs(distanceToWall) > 0.8f)
|
|
{
|
|
DetachFromWall(false);
|
|
return;
|
|
}
|
|
|
|
if (inputs.JumpPressed)
|
|
{
|
|
DetachFromWall(true);
|
|
}
|
|
}
|
|
|
|
private bool IsWallStillPresent()
|
|
{
|
|
if (_motor == null) return false;
|
|
|
|
Vector3 characterCenter = _motor.TransientPosition;
|
|
Vector3 characterForward = _motor.CharacterForward;
|
|
Vector3 characterRight = _motor.CharacterRight;
|
|
Vector3 characterUp = _motor.CharacterUp;
|
|
|
|
float checkDistance = 0.45f;
|
|
Vector3 rayOrigin = characterCenter + characterUp * 0.2f;
|
|
Vector3 rayDir = -_wallNormal;
|
|
|
|
Vector3[] offsets = {
|
|
Vector3.zero,
|
|
characterUp * 0.3f,
|
|
-characterUp * 0.3f,
|
|
characterRight * 0.2f,
|
|
-characterRight * 0.2f
|
|
};
|
|
|
|
foreach (var offset in offsets)
|
|
{
|
|
if (Physics.Raycast(rayOrigin + offset, rayDir, out RaycastHit hit, checkDistance,
|
|
_motor.CollidableLayers, QueryTriggerInteraction.Ignore))
|
|
{
|
|
float dotWithUp = Mathf.Abs(Vector3.Dot(hit.normal, characterUp));
|
|
if (dotWithUp < _surfaceAngleCos)
|
|
{
|
|
_wallNormal = hit.normal.normalized;
|
|
_wallContactPoint = hit.point;
|
|
|
|
/*_wallTangentUp = Vector3.ProjectOnPlane(_motor.CharacterUp, _wallNormal).normalized;
|
|
if (_wallTangentUp.sqrMagnitude < 0.01f) _wallTangentUp = Vector3.up;
|
|
_wallTangentRight = Vector3.Cross(_wallNormal, _wallTangentUp).normalized;
|
|
*/
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void DetachFromWall(bool withImpulse)
|
|
{
|
|
if (!_isWallRunning) return;
|
|
|
|
_isWallRunning = false;
|
|
|
|
if (withImpulse && _motor != null)
|
|
{
|
|
Vector3 detachImpulse = -_wallNormal * WallDetachImpulse;
|
|
_motor.BaseVelocity += detachImpulse;
|
|
}
|
|
}
|
|
|
|
public Vector3 GetWallNormal() => _wallNormal;
|
|
public bool TryGetWallTangents(out Vector3 up, out Vector3 right)
|
|
{
|
|
if (!_isWallRunning)
|
|
{
|
|
up = right = Vector3.zero;
|
|
return false;
|
|
}
|
|
up = _wallTangentUp;
|
|
right = _wallTangentRight;
|
|
return true;
|
|
}
|
|
|
|
public bool CanWallRun()
|
|
{
|
|
return !_isWallRunning && !_motor.GroundingStatus.IsStableOnGround;
|
|
}
|
|
|
|
public void PerformWallJump(CharacterInputs inputs, Quaternion cameraRotation)
|
|
{
|
|
if (!_isWallRunning || _motor == null) return;
|
|
|
|
_isWallRunning = false;
|
|
|
|
Vector3 wallAway = _wallNormal;
|
|
Vector3 wallUp = _wallTangentUp;
|
|
|
|
Vector3 playerForward = cameraRotation * Vector3.forward;
|
|
|
|
Vector3 forwardInWallPlane = Vector3.ProjectOnPlane(playerForward, _wallNormal).normalized;
|
|
if (forwardInWallPlane.sqrMagnitude < 0.01f) forwardInWallPlane = _wallTangentRight;
|
|
|
|
Vector3 jumpDirection =
|
|
forwardInWallPlane * 0.2f +
|
|
wallAway * 0.4f +
|
|
wallUp * 0.4f;
|
|
|
|
jumpDirection = jumpDirection.normalized;
|
|
|
|
float wallJumpForce = WallDetachImpulse * 2f;
|
|
|
|
Vector3 jumpImpulse = jumpDirection * wallJumpForce;
|
|
|
|
_motor.BaseVelocity += jumpImpulse;
|
|
|
|
_motor.SetPosition(_motor.TransientPosition + wallAway * 0.07f);
|
|
|
|
if (_motor.CharacterController is CustomCharacterController controller)
|
|
{
|
|
controller.BlockAbilities(0.12f);
|
|
}
|
|
}
|
|
}
|
|
} |