set up basic walking, running, jumping

TODO: crouching, climbing
This commit is contained in:
2025-12-04 18:14:44 -05:00
parent 00efc742b4
commit f25c48ee4c
9 changed files with 7447 additions and 19 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,15 @@
using UnityEngine;
namespace Boxfriend
{
public class ConstantStrings
{
public const string PlayerActionMap = "Player";
public const string InteractInput = "Interact";
public const string JumpInput = "Jump";
public const string MoveInput = "Move";
public const string CrouchInput = "Crouch";
public const string SprintInput = "Sprint";
public const string LookInput = "Look";
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bb691a8ce5d40ee46897a94cf9324b80

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ffc1fd0c06f8ee049864fababfc75429
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,47 @@
using System;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Boxfriend.Input
{
public class InputDispatcher : MonoBehaviour
{
public event Action<Vector2> OnMove;
public event Action<Vector2> OnLook;
public event Action<bool> OnSprint;
public event Action<bool> OnCrouch;
public event Action OnJump;
public event Action OnInteract;
private InputActionMap _inputActions;
private void Awake()
{
_inputActions = InputSystem.actions.FindActionMap(ConstantStrings.PlayerActionMap);
Subscribe();
}
private void Subscribe()
{
_inputActions.FindAction(ConstantStrings.InteractInput).performed += _ => OnInteract?.Invoke();
_inputActions.FindAction(ConstantStrings.JumpInput).performed += _ => OnJump?.Invoke();
_inputActions.FindAction(ConstantStrings.MoveInput).performed += ctx => OnMove?.Invoke(ctx.ReadValue<Vector2>());
_inputActions.FindAction(ConstantStrings.MoveInput).canceled += ctx => OnMove?.Invoke(ctx.ReadValue<Vector2>());
_inputActions.FindAction(ConstantStrings.LookInput).performed += ctx => OnLook?.Invoke(ctx.ReadValue<Vector2>());
_inputActions.FindAction(ConstantStrings.SprintInput).performed += ctx => OnSprint?.Invoke(ctx.ReadValueAsButton());
_inputActions.FindAction(ConstantStrings.SprintInput).canceled += ctx => OnSprint?.Invoke(ctx.ReadValueAsButton());
_inputActions.FindAction(ConstantStrings.CrouchInput).performed += ctx => OnCrouch?.Invoke(ctx.ReadValueAsButton());
_inputActions.FindAction(ConstantStrings.CrouchInput).canceled += ctx => OnCrouch?.Invoke(ctx.ReadValueAsButton());
}
private void OnEnable () => _inputActions.Enable();
private void OnDisable () => _inputActions.Disable();
private void OnDestroy () => _inputActions.Dispose();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: aa5ffe305bd55df4da17e57cf5f34574

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7f9c675b73c66254baa0d7f1beedcb87
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,99 @@
using Boxfriend.Extensions;
using Boxfriend.Input;
using KinematicCharacterController;
using UnityEngine;
namespace Boxfriend.Player
{
[RequireComponent(typeof(InputDispatcher))]
[RequireComponent(typeof(KinematicCharacterMotor))]
public class PlayerMotor : MonoBehaviour, ICharacterController
{
[SerializeField, InspectorOnly] private KinematicCharacterMotor _characterMotor;
[SerializeField, InspectorOnly] private InputDispatcher _input;
private bool _isSprinting;
private float _yRotation;
private float _gravityVelocity;
private Vector3 _moveDirection;
private Vector3 _groundNormal = Vector3.up;
[SerializeField, InspectorOnly] private float _moveSpeed;
[SerializeField, InspectorOnly] private float _defaultGravity;
[SerializeField, InspectorOnly] private float _jumpForce;
private void Awake ()
{
_input.OnLook += OnLook;
_input.OnMove += OnMove;
_input.OnJump += OnJump;
_input.OnSprint += OnSprint;
_characterMotor.CharacterController = this;
}
private void OnLook (Vector2 delta) => _yRotation = Mathf.Repeat(_yRotation + delta.x, 360);
private void OnMove (Vector2 inputDir) => _moveDirection = inputDir.To3D();
private void OnSprint (bool sprinting) => _isSprinting = sprinting;
private void OnJump ()
{
if (!_characterMotor.GroundingStatus.IsStableOnGround)
return;
_characterMotor.ForceUnground();
_groundNormal = Vector3.up;
_gravityVelocity = _jumpForce;
}
public void UpdateRotation (ref Quaternion currentRotation, float deltaTime) => currentRotation = Quaternion.Euler(0, _yRotation, 0);
public void UpdateVelocity (ref Vector3 currentVelocity, float deltaTime)
{
if (!_characterMotor.GroundingStatus.IsStableOnGround)
{
_gravityVelocity += _defaultGravity * deltaTime;
currentVelocity.y = _gravityVelocity;
return;
}
_gravityVelocity = 0;
var moveDir = transform.TransformDirection(_moveDirection);
var direction = _characterMotor.GetDirectionTangentToSurface(moveDir, _groundNormal);
direction *= _isSprinting ? _moveSpeed * 1.5f : _moveSpeed;
direction.y += _gravityVelocity;
currentVelocity = direction;
}
public void BeforeCharacterUpdate (float deltaTime) { }
public void PostGroundingUpdate (float deltaTime) { }
public void AfterCharacterUpdate (float deltaTime) { }
public bool IsColliderValidForCollisions (Collider coll) => coll != _characterMotor.Capsule;
public void OnGroundHit (Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint,
ref HitStabilityReport hitStabilityReport) => _groundNormal = hitNormal;
public void OnMovementHit (Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint,
ref HitStabilityReport hitStabilityReport)
{ }
public void ProcessHitStabilityReport (Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, Vector3 atCharacterPosition,
Quaternion atCharacterRotation, ref HitStabilityReport hitStabilityReport)
{ }
public void OnDiscreteCollisionDetected (Collider hitCollider) { }
private void Reset ()
{
_characterMotor = GetComponent<KinematicCharacterMotor>();
_input = GetComponent<InputDispatcher>();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: aae104d826c777940970b41deaa82b38