Touch up touch input

This commit is contained in:
2025-09-22 15:01:53 +02:00
parent da07f778c3
commit 1a9c82f5d7
2 changed files with 82 additions and 23 deletions

View File

@@ -8,9 +8,19 @@ namespace Minigames.DivingForPictures
/// </summary>
public class PlayerController : MonoBehaviour, ITouchInputConsumer
{
[Header("Tap Movement")]
[Tooltip("Maximum distance the player can move from a single tap")]
[SerializeField] private float tapMaxDistance = 0.5f;
[Tooltip("How quickly the tap impulse fades (higher = faster stop)")]
[SerializeField] private float tapDecelerationRate = 5.0f;
private float _targetFingerX;
private bool _isTouchActive;
private float _originY;
// Tap impulse system variables
private float _tapImpulseStrength = 0f;
private float _tapDirection = 0f;
void Awake()
{
@@ -27,13 +37,24 @@ namespace Minigames.DivingForPictures
}
/// <summary>
/// Handles tap input. Moves to the tapped X position.
/// Handles tap input. Applies an impulse in the tapped direction.
/// </summary>
public void OnTap(Vector2 worldPosition)
{
// Debug.Log($"[EndlessDescenderController] OnTap at {worldPosition}");
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
_isTouchActive = true;
float targetX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
// Calculate tap direction (+1 for right, -1 for left)
_tapDirection = Mathf.Sign(targetX - transform.position.x);
// Set impulse strength to full
_tapImpulseStrength = 1.0f;
// Store target X for animation purposes
_targetFingerX = targetX;
// Do not set _isTouchActive for taps anymore
// _isTouchActive = true; - Removed to prevent continuous movement
}
/// <summary>
@@ -66,21 +87,59 @@ namespace Minigames.DivingForPictures
void Update()
{
if (!_isTouchActive) return;
float currentX = transform.position.x;
float lerpSpeed = GameManager.Instance.EndlessDescenderLerpSpeed;
float maxOffset = GameManager.Instance.EndlessDescenderMaxOffset;
float exponent = GameManager.Instance.EndlessDescenderSpeedExponent;
float targetX = _targetFingerX;
float offset = targetX - currentX;
offset = Mathf.Clamp(offset, -maxOffset, maxOffset);
float absOffset = Mathf.Abs(offset);
float t = Mathf.Pow(absOffset / maxOffset, exponent); // Non-linear drop-off
float moveStep = Mathf.Sign(offset) * maxOffset * t * Time.deltaTime * lerpSpeed;
// Prevent overshooting
moveStep = Mathf.Clamp(moveStep, -absOffset, absOffset);
float newX = currentX + moveStep;
newX = Mathf.Clamp(newX, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
// Handle hold movement
if (_isTouchActive)
{
float currentX = transform.position.x;
float lerpSpeed = GameManager.Instance.EndlessDescenderLerpSpeed;
float maxOffset = GameManager.Instance.EndlessDescenderMaxOffset;
float exponent = GameManager.Instance.EndlessDescenderSpeedExponent;
float targetX = _targetFingerX;
float offset = targetX - currentX;
offset = Mathf.Clamp(offset, -maxOffset, maxOffset);
float absOffset = Mathf.Abs(offset);
float t = Mathf.Pow(absOffset / maxOffset, exponent); // Non-linear drop-off
float moveStep = Mathf.Sign(offset) * maxOffset * t * Time.deltaTime * lerpSpeed;
// Prevent overshooting
moveStep = Mathf.Clamp(moveStep, -absOffset, absOffset);
float newX = currentX + moveStep;
newX = Mathf.Clamp(newX, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
UpdatePosition(newX);
}
// Handle tap impulse movement
else if (_tapImpulseStrength > 0)
{
float currentX = transform.position.x;
float maxOffset = GameManager.Instance.EndlessDescenderMaxOffset;
float lerpSpeed = GameManager.Instance.EndlessDescenderLerpSpeed;
// Calculate move distance based on impulse strength
float moveDistance = maxOffset * _tapImpulseStrength * Time.deltaTime * lerpSpeed;
// Limit total movement from single tap
moveDistance = Mathf.Min(moveDistance, tapMaxDistance * _tapImpulseStrength);
// Apply movement in tap direction
float newX = currentX + (moveDistance * _tapDirection);
newX = Mathf.Clamp(newX, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
// Reduce impulse strength over time
_tapImpulseStrength -= Time.deltaTime * tapDecelerationRate;
if (_tapImpulseStrength < 0.01f)
{
_tapImpulseStrength = 0f;
}
UpdatePosition(newX);
}
}
/// <summary>
/// Updates the player's position with the given X coordinate
/// </summary>
private void UpdatePosition(float newX)
{
float newY = _originY;
// Add vertical offset from WobbleBehavior if present
WobbleBehavior wobble = GetComponent<WobbleBehavior>();