From c46036dce6e37f955a9e2cf9a3807d02ef08e29a Mon Sep 17 00:00:00 2001 From: Michal Pikulski Date: Tue, 7 Oct 2025 10:11:47 +0200 Subject: [PATCH] Fix speed mismatch between direct and tap movement modes --- .../Core/Settings/PlayerFollowerSettings.cs | 2 + .../Core/Settings/SettingsInterfaces.cs | 1 + Assets/Scripts/Input/PlayerTouchController.cs | 41 ++++++++++++------- Assets/Settings/PlayerFollowerSettings.asset | 1 + 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Assets/Scripts/Core/Settings/PlayerFollowerSettings.cs b/Assets/Scripts/Core/Settings/PlayerFollowerSettings.cs index 8e1d59a2..2d25d74d 100644 --- a/Assets/Scripts/Core/Settings/PlayerFollowerSettings.cs +++ b/Assets/Scripts/Core/Settings/PlayerFollowerSettings.cs @@ -10,6 +10,7 @@ namespace AppleHills.Core.Settings { [Header("Player Settings")] [SerializeField] private float moveSpeed = 5f; + [SerializeField] private float moveAcceleration = 10000f; [SerializeField] private float stopDistance = 0.1f; [SerializeField] private bool useRigidbody = true; [SerializeField] private HoldMovementMode defaultHoldMovementMode = HoldMovementMode.Pathfinding; @@ -29,6 +30,7 @@ namespace AppleHills.Core.Settings // IPlayerFollowerSettings implementation public float MoveSpeed => moveSpeed; + public float MaxAcceleration => moveAcceleration; public float StopDistance => stopDistance; public bool UseRigidbody => useRigidbody; public HoldMovementMode DefaultHoldMovementMode => defaultHoldMovementMode; diff --git a/Assets/Scripts/Core/Settings/SettingsInterfaces.cs b/Assets/Scripts/Core/Settings/SettingsInterfaces.cs index 5cc343a8..440594bd 100644 --- a/Assets/Scripts/Core/Settings/SettingsInterfaces.cs +++ b/Assets/Scripts/Core/Settings/SettingsInterfaces.cs @@ -10,6 +10,7 @@ namespace AppleHills.Core.Settings { // Player settings float MoveSpeed { get; } + float MaxAcceleration { get; } // Added new property for player acceleration float StopDistance { get; } bool UseRigidbody { get; } HoldMovementMode DefaultHoldMovementMode { get; } diff --git a/Assets/Scripts/Input/PlayerTouchController.cs b/Assets/Scripts/Input/PlayerTouchController.cs index 45fbe3e2..9a9f5378 100644 --- a/Assets/Scripts/Input/PlayerTouchController.cs +++ b/Assets/Scripts/Input/PlayerTouchController.cs @@ -21,6 +21,9 @@ namespace Input public LayerMask obstacleMask; public float colliderRadius = 0.5f; + // --- Settings Reference --- + private IPlayerFollowerSettings _settings; + // --- Movement Events --- private bool _isMoving = false; public bool IsMoving => _isMoving; @@ -63,6 +66,9 @@ namespace Input spriteRenderer = artTransform.GetComponent(); if (spriteRenderer == null) spriteRenderer = GetComponentInChildren(); + + // Initialize settings reference using GetSettingsObject + _settings = GameManager.GetSettingsObject(); } void Start() @@ -99,7 +105,7 @@ namespace Input Debug.Log($"[PlayerTouchController] OnHoldStart at {worldPosition}"); lastHoldPosition = worldPosition; isHolding = true; - if (GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Pathfinding && + if (_settings.DefaultHoldMovementMode == HoldMovementMode.Pathfinding && aiPath != null) { aiPath.enabled = true; @@ -120,7 +126,7 @@ namespace Input { if (!isHolding) return; lastHoldPosition = worldPosition; - if (GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct) + if (_settings.DefaultHoldMovementMode == HoldMovementMode.Direct) { if (aiPath != null && aiPath.enabled) aiPath.enabled = false; MoveDirectlyTo(worldPosition); @@ -136,7 +142,7 @@ namespace Input Debug.Log($"[PlayerTouchController] OnHoldEnd at {worldPosition}"); isHolding = false; directMoveVelocity = Vector3.zero; - if (aiPath != null && GameManager.Instance.DefaultHoldMovementMode == + if (aiPath != null && _settings.DefaultHoldMovementMode == HoldMovementMode.Pathfinding) { if (pathfindingDragCoroutine != null) @@ -146,7 +152,7 @@ namespace Input } } - if (aiPath != null && GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct) + if (aiPath != null && _settings.DefaultHoldMovementMode == HoldMovementMode.Direct) { aiPath.enabled = false; } @@ -160,7 +166,9 @@ namespace Input if (aiPath != null) { aiPath.destination = worldPosition; - aiPath.maxSpeed = GameManager.Instance.MoveSpeed; + // Apply both speed and acceleration from settings + aiPath.maxSpeed = _settings.MoveSpeed; + aiPath.maxAcceleration = _settings.MaxAcceleration; aiPath.canMove = true; aiPath.isStopped = false; } @@ -179,8 +187,11 @@ namespace Input Vector3 target = new Vector3(worldPosition.x, worldPosition.y, current.z); Vector3 toTarget = (target - current); Vector3 direction = toTarget.normalized; - float maxSpeed = aiPath.maxSpeed; - float acceleration = aiPath.maxAcceleration; + + // Get speed and acceleration directly from settings + float maxSpeed = _settings.MoveSpeed; + float acceleration = _settings.MaxAcceleration; + directMoveVelocity = Vector3.MoveTowards(directMoveVelocity, direction * maxSpeed, acceleration * Time.deltaTime); if (directMoveVelocity.magnitude > maxSpeed) { @@ -242,15 +253,16 @@ namespace Input { float normalizedSpeed = 0f; Vector3 velocity = Vector3.zero; + float maxSpeed = _settings.MoveSpeed; - if (isHolding && GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct) + if (isHolding && _settings.DefaultHoldMovementMode == HoldMovementMode.Direct) { - normalizedSpeed = directMoveVelocity.magnitude / aiPath.maxSpeed; + normalizedSpeed = directMoveVelocity.magnitude / maxSpeed; velocity = directMoveVelocity; } else if (aiPath.enabled) { - normalizedSpeed = aiPath.velocity.magnitude / aiPath.maxSpeed; + normalizedSpeed = aiPath.velocity.magnitude / maxSpeed; velocity = aiPath.velocity; } @@ -288,7 +300,7 @@ namespace Input bool isCurrentlyMoving = false; // Check direct movement - if (isHolding && GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct) + if (isHolding && _settings.DefaultHoldMovementMode == HoldMovementMode.Direct) { isCurrentlyMoving = directMoveVelocity.sqrMagnitude > 0.001f; } @@ -356,7 +368,7 @@ namespace Input interruptMoveTo = true; isHolding = false; directMoveVelocity = Vector3.zero; - if (GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct && aiPath != null) + if (_settings.DefaultHoldMovementMode == HoldMovementMode.Direct && aiPath != null) aiPath.enabled = false; OnMoveToCancelled?.Invoke(); } @@ -369,7 +381,8 @@ namespace Input if (aiPath != null) { aiPath.destination = target; - aiPath.maxSpeed = GameManager.Instance.MoveSpeed; + aiPath.maxSpeed = _settings.MoveSpeed; + aiPath.maxAcceleration = _settings.MaxAcceleration; } while (!interruptMoveTo) @@ -377,7 +390,7 @@ namespace Input Vector2 current2D = new Vector2(transform.position.x, transform.position.y); Vector2 target2D = new Vector2(target.x, target.y); float dist = Vector2.Distance(current2D, target2D); - if (dist <= GameManager.Instance.StopDistance + 0.2f) + if (dist <= _settings.StopDistance + 0.2f) { break; } diff --git a/Assets/Settings/PlayerFollowerSettings.asset b/Assets/Settings/PlayerFollowerSettings.asset index 95991414..296e0955 100644 --- a/Assets/Settings/PlayerFollowerSettings.asset +++ b/Assets/Settings/PlayerFollowerSettings.asset @@ -13,6 +13,7 @@ MonoBehaviour: m_Name: PlayerFollowerSettings m_EditorClassIdentifier: moveSpeed: 25 + moveAcceleration: 10000 stopDistance: 2 useRigidbody: 1 defaultHoldMovementMode: 1