Fix speed mismatch between direct and tap movement modes
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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<SpriteRenderer>();
|
||||
if (spriteRenderer == null)
|
||||
spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||
|
||||
// Initialize settings reference using GetSettingsObject
|
||||
_settings = GameManager.GetSettingsObject<IPlayerFollowerSettings>();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ MonoBehaviour:
|
||||
m_Name: PlayerFollowerSettings
|
||||
m_EditorClassIdentifier:
|
||||
moveSpeed: 25
|
||||
moveAcceleration: 10000
|
||||
stopDistance: 2
|
||||
useRigidbody: 1
|
||||
defaultHoldMovementMode: 1
|
||||
|
||||
Reference in New Issue
Block a user