Implement jump mechanics and refine ground detection; adjust gravity application for improved player control

This commit is contained in:
2025-12-13 02:46:36 +01:00
parent 471cf45df3
commit 0e86d2f4f9
282 changed files with 80 additions and 133181 deletions

View File

@@ -23,10 +23,6 @@ namespace Infrastructure.Unity
[SerializeField] private HunterNpcController hunterNpcPrefab;
[SerializeField] private FloorVisibilityManager floorVisibilityManager;
[Header("Level Generation")]
[SerializeField] private int floorsCount = 3;
[SerializeField] private float floorHeightDistance = 15f;
[Header("Ui")]
[SerializeField] private TMP_Text scoreText;
[SerializeField] private TMP_Text highScoreText;
@@ -70,7 +66,9 @@ namespace Infrastructure.Unity
// Set Theme based on High Score
ThemeManager.CurrentTheme = ThemeManager.GetTheme(_gameSession.HighScore);
var floorsCount = levelGenerator ? levelGenerator.FloorsCount : 1;
if (levelGenerator)
{
levelGenerator.Generate(soundManager, _allTiles, _tileViews, cameraController, rumbleManager);
@@ -111,9 +109,12 @@ namespace Infrastructure.Unity
// Calculate current floor index based on Y height (inverse logic from Generator)
// Note: Generator uses negative offsets: 0, -15, -30.
// So Floor 0 is at Y=0. Floor 1 is at Y=-15.
var heightDist = levelGenerator.FloorHeightDistance;
var maxFloors = levelGenerator.FloorsCount;
var rawFloor = Mathf.RoundToInt(-playerY / floorHeightDistance);
_currentPlayerFloorIndex = Mathf.Clamp(rawFloor, 0, floorsCount - 1);
var rawFloor = Mathf.RoundToInt(-playerY / heightDist);
_currentPlayerFloorIndex = Mathf.Clamp(rawFloor, 0, maxFloors - 1);
_gameSession.SetPlayerFloor(_currentPlayerFloorIndex);

View File

@@ -15,14 +15,19 @@ namespace Infrastructure.Unity
private float moveSpeed = 8f;
[SerializeField] private float maxVelocityChange = 10f;
[SerializeField] private float snapForce = 15f;
[Header("Jump Settings")]
[SerializeField] private float jumpForce = 12f; // Adjusted for snappy feel
[SerializeField] private float coyoteTime = 0.1f; // Grace period after falling
[SerializeField] private float gravityMultiplier = 2.5f; // Stronger gravity for less "floaty" feel
[Header("Controls")]
[SerializeField] private bool useCameraRelativeMovement = true;
[Header("Interaction")]
[SerializeField] private LayerMask tileLayer;
[SerializeField] private float groundCheckDistance = 1.5f;
[SerializeField] private float groundCheckDistance = 1.1f; // Slightly more than half player height
[Self][SerializeField] private Rigidbody rb;
[Self][SerializeField] private MeshRenderer meshRenderer;
@@ -35,6 +40,10 @@ namespace Infrastructure.Unity
private static readonly int EmissionColorProperty = Shader.PropertyToID("_EmissionColor");
private Color _defaultColor = Color.white;
private bool _isGrounded;
private float _coyoteTimeCounter;
private bool _jumpPressed;
public Rigidbody Rigidbody => rb;
public StatusManager Status { get; private set; }
@@ -44,12 +53,14 @@ namespace Infrastructure.Unity
_actions.Player.Move.performed += OnMovePerformed;
_actions.Player.Move.canceled += OnMoveCanceled;
_actions.Player.Jump.performed += OnJumpPerformed;
}
private void OnDisable()
{
_actions.Player.Move.performed -= OnMovePerformed;
_actions.Player.Move.canceled -= OnMoveCanceled;
_actions.Player.Jump.performed -= OnJumpPerformed;
_actions.Player.Disable();
}
@@ -65,6 +76,7 @@ namespace Infrastructure.Unity
}
rb.freezeRotation = true;
rb.useGravity = false;
_propBlock = new MaterialPropertyBlock();
if (meshRenderer.material.HasProperty(ColorProperty))
@@ -76,16 +88,64 @@ namespace Infrastructure.Unity
private void Update()
{
Status.Tick(Time.deltaTime);
rb.useGravity = !Status.CurrentCapabilities.CanHover;
UpdateVisuals();
if (_isGrounded)
{
_coyoteTimeCounter = coyoteTime;
}
else
{
_coyoteTimeCounter -= Time.deltaTime;
}
if (_jumpPressed)
{
_jumpPressed = false;
if (_coyoteTimeCounter > 0f || Status.CurrentCapabilities.CanHover)
{
PerformJump();
}
}
}
private void FixedUpdate()
{
CheckGround();
HandleMovement();
DetectGround();
ApplyGravity();
// Interaction with tiles (Decay Logic)
if (_isGrounded && Status.CurrentCapabilities.CanTriggerDecay)
{
InteractWithGround();
}
}
private void CheckGround()
{
_isGrounded = Physics.Raycast(transform.position, Vector3.down, groundCheckDistance, tileLayer);
}
private void PerformJump()
{
var vel = rb.linearVelocity;
vel.y = 0;
rb.linearVelocity = vel;
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
_coyoteTimeCounter = 0f;
_isGrounded = false;
}
private void ApplyGravity()
{
if (Status.CurrentCapabilities.CanHover) return;
var gravity = Physics.gravity.y * gravityMultiplier;
rb.AddForce(Vector3.up * gravity, ForceMode.Acceleration);
}
private void HandleMovement()
@@ -162,10 +222,8 @@ namespace Infrastructure.Unity
rb.AddForce(correction, ForceMode.Acceleration);
}
private void DetectGround()
private void InteractWithGround()
{
if (!Status.CurrentCapabilities.CanTriggerDecay) return;
if (Physics.Raycast(transform.position, Vector3.down, out var hit, groundCheckDistance, tileLayer))
{
if (hit.collider.TryGetComponent<TileViewAdapter>(out var tileAdapter))
@@ -184,6 +242,11 @@ namespace Infrastructure.Unity
{
_moveInput = Vector2.zero;
}
private void OnJumpPerformed(InputAction.CallbackContext ctx)
{
if (ctx.performed) _jumpPressed = true;
}
private void UpdateVisuals()
{