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")]
|
[Header("Player Settings")]
|
||||||
[SerializeField] private float moveSpeed = 5f;
|
[SerializeField] private float moveSpeed = 5f;
|
||||||
|
[SerializeField] private float moveAcceleration = 10000f;
|
||||||
[SerializeField] private float stopDistance = 0.1f;
|
[SerializeField] private float stopDistance = 0.1f;
|
||||||
[SerializeField] private bool useRigidbody = true;
|
[SerializeField] private bool useRigidbody = true;
|
||||||
[SerializeField] private HoldMovementMode defaultHoldMovementMode = HoldMovementMode.Pathfinding;
|
[SerializeField] private HoldMovementMode defaultHoldMovementMode = HoldMovementMode.Pathfinding;
|
||||||
@@ -29,6 +30,7 @@ namespace AppleHills.Core.Settings
|
|||||||
|
|
||||||
// IPlayerFollowerSettings implementation
|
// IPlayerFollowerSettings implementation
|
||||||
public float MoveSpeed => moveSpeed;
|
public float MoveSpeed => moveSpeed;
|
||||||
|
public float MaxAcceleration => moveAcceleration;
|
||||||
public float StopDistance => stopDistance;
|
public float StopDistance => stopDistance;
|
||||||
public bool UseRigidbody => useRigidbody;
|
public bool UseRigidbody => useRigidbody;
|
||||||
public HoldMovementMode DefaultHoldMovementMode => defaultHoldMovementMode;
|
public HoldMovementMode DefaultHoldMovementMode => defaultHoldMovementMode;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace AppleHills.Core.Settings
|
|||||||
{
|
{
|
||||||
// Player settings
|
// Player settings
|
||||||
float MoveSpeed { get; }
|
float MoveSpeed { get; }
|
||||||
|
float MaxAcceleration { get; } // Added new property for player acceleration
|
||||||
float StopDistance { get; }
|
float StopDistance { get; }
|
||||||
bool UseRigidbody { get; }
|
bool UseRigidbody { get; }
|
||||||
HoldMovementMode DefaultHoldMovementMode { get; }
|
HoldMovementMode DefaultHoldMovementMode { get; }
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ namespace Input
|
|||||||
public LayerMask obstacleMask;
|
public LayerMask obstacleMask;
|
||||||
public float colliderRadius = 0.5f;
|
public float colliderRadius = 0.5f;
|
||||||
|
|
||||||
|
// --- Settings Reference ---
|
||||||
|
private IPlayerFollowerSettings _settings;
|
||||||
|
|
||||||
// --- Movement Events ---
|
// --- Movement Events ---
|
||||||
private bool _isMoving = false;
|
private bool _isMoving = false;
|
||||||
public bool IsMoving => _isMoving;
|
public bool IsMoving => _isMoving;
|
||||||
@@ -63,6 +66,9 @@ namespace Input
|
|||||||
spriteRenderer = artTransform.GetComponent<SpriteRenderer>();
|
spriteRenderer = artTransform.GetComponent<SpriteRenderer>();
|
||||||
if (spriteRenderer == null)
|
if (spriteRenderer == null)
|
||||||
spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||||
|
|
||||||
|
// Initialize settings reference using GetSettingsObject
|
||||||
|
_settings = GameManager.GetSettingsObject<IPlayerFollowerSettings>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
@@ -99,7 +105,7 @@ namespace Input
|
|||||||
Debug.Log($"[PlayerTouchController] OnHoldStart at {worldPosition}");
|
Debug.Log($"[PlayerTouchController] OnHoldStart at {worldPosition}");
|
||||||
lastHoldPosition = worldPosition;
|
lastHoldPosition = worldPosition;
|
||||||
isHolding = true;
|
isHolding = true;
|
||||||
if (GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Pathfinding &&
|
if (_settings.DefaultHoldMovementMode == HoldMovementMode.Pathfinding &&
|
||||||
aiPath != null)
|
aiPath != null)
|
||||||
{
|
{
|
||||||
aiPath.enabled = true;
|
aiPath.enabled = true;
|
||||||
@@ -120,7 +126,7 @@ namespace Input
|
|||||||
{
|
{
|
||||||
if (!isHolding) return;
|
if (!isHolding) return;
|
||||||
lastHoldPosition = worldPosition;
|
lastHoldPosition = worldPosition;
|
||||||
if (GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct)
|
if (_settings.DefaultHoldMovementMode == HoldMovementMode.Direct)
|
||||||
{
|
{
|
||||||
if (aiPath != null && aiPath.enabled) aiPath.enabled = false;
|
if (aiPath != null && aiPath.enabled) aiPath.enabled = false;
|
||||||
MoveDirectlyTo(worldPosition);
|
MoveDirectlyTo(worldPosition);
|
||||||
@@ -136,7 +142,7 @@ namespace Input
|
|||||||
Debug.Log($"[PlayerTouchController] OnHoldEnd at {worldPosition}");
|
Debug.Log($"[PlayerTouchController] OnHoldEnd at {worldPosition}");
|
||||||
isHolding = false;
|
isHolding = false;
|
||||||
directMoveVelocity = Vector3.zero;
|
directMoveVelocity = Vector3.zero;
|
||||||
if (aiPath != null && GameManager.Instance.DefaultHoldMovementMode ==
|
if (aiPath != null && _settings.DefaultHoldMovementMode ==
|
||||||
HoldMovementMode.Pathfinding)
|
HoldMovementMode.Pathfinding)
|
||||||
{
|
{
|
||||||
if (pathfindingDragCoroutine != null)
|
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;
|
aiPath.enabled = false;
|
||||||
}
|
}
|
||||||
@@ -160,7 +166,9 @@ namespace Input
|
|||||||
if (aiPath != null)
|
if (aiPath != null)
|
||||||
{
|
{
|
||||||
aiPath.destination = worldPosition;
|
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.canMove = true;
|
||||||
aiPath.isStopped = false;
|
aiPath.isStopped = false;
|
||||||
}
|
}
|
||||||
@@ -179,8 +187,11 @@ namespace Input
|
|||||||
Vector3 target = new Vector3(worldPosition.x, worldPosition.y, current.z);
|
Vector3 target = new Vector3(worldPosition.x, worldPosition.y, current.z);
|
||||||
Vector3 toTarget = (target - current);
|
Vector3 toTarget = (target - current);
|
||||||
Vector3 direction = toTarget.normalized;
|
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);
|
directMoveVelocity = Vector3.MoveTowards(directMoveVelocity, direction * maxSpeed, acceleration * Time.deltaTime);
|
||||||
if (directMoveVelocity.magnitude > maxSpeed)
|
if (directMoveVelocity.magnitude > maxSpeed)
|
||||||
{
|
{
|
||||||
@@ -242,15 +253,16 @@ namespace Input
|
|||||||
{
|
{
|
||||||
float normalizedSpeed = 0f;
|
float normalizedSpeed = 0f;
|
||||||
Vector3 velocity = Vector3.zero;
|
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;
|
velocity = directMoveVelocity;
|
||||||
}
|
}
|
||||||
else if (aiPath.enabled)
|
else if (aiPath.enabled)
|
||||||
{
|
{
|
||||||
normalizedSpeed = aiPath.velocity.magnitude / aiPath.maxSpeed;
|
normalizedSpeed = aiPath.velocity.magnitude / maxSpeed;
|
||||||
velocity = aiPath.velocity;
|
velocity = aiPath.velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,7 +300,7 @@ namespace Input
|
|||||||
bool isCurrentlyMoving = false;
|
bool isCurrentlyMoving = false;
|
||||||
|
|
||||||
// Check direct movement
|
// Check direct movement
|
||||||
if (isHolding && GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct)
|
if (isHolding && _settings.DefaultHoldMovementMode == HoldMovementMode.Direct)
|
||||||
{
|
{
|
||||||
isCurrentlyMoving = directMoveVelocity.sqrMagnitude > 0.001f;
|
isCurrentlyMoving = directMoveVelocity.sqrMagnitude > 0.001f;
|
||||||
}
|
}
|
||||||
@@ -356,7 +368,7 @@ namespace Input
|
|||||||
interruptMoveTo = true;
|
interruptMoveTo = true;
|
||||||
isHolding = false;
|
isHolding = false;
|
||||||
directMoveVelocity = Vector3.zero;
|
directMoveVelocity = Vector3.zero;
|
||||||
if (GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct && aiPath != null)
|
if (_settings.DefaultHoldMovementMode == HoldMovementMode.Direct && aiPath != null)
|
||||||
aiPath.enabled = false;
|
aiPath.enabled = false;
|
||||||
OnMoveToCancelled?.Invoke();
|
OnMoveToCancelled?.Invoke();
|
||||||
}
|
}
|
||||||
@@ -369,7 +381,8 @@ namespace Input
|
|||||||
if (aiPath != null)
|
if (aiPath != null)
|
||||||
{
|
{
|
||||||
aiPath.destination = target;
|
aiPath.destination = target;
|
||||||
aiPath.maxSpeed = GameManager.Instance.MoveSpeed;
|
aiPath.maxSpeed = _settings.MoveSpeed;
|
||||||
|
aiPath.maxAcceleration = _settings.MaxAcceleration;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!interruptMoveTo)
|
while (!interruptMoveTo)
|
||||||
@@ -377,7 +390,7 @@ namespace Input
|
|||||||
Vector2 current2D = new Vector2(transform.position.x, transform.position.y);
|
Vector2 current2D = new Vector2(transform.position.x, transform.position.y);
|
||||||
Vector2 target2D = new Vector2(target.x, target.y);
|
Vector2 target2D = new Vector2(target.x, target.y);
|
||||||
float dist = Vector2.Distance(current2D, target2D);
|
float dist = Vector2.Distance(current2D, target2D);
|
||||||
if (dist <= GameManager.Instance.StopDistance + 0.2f)
|
if (dist <= _settings.StopDistance + 0.2f)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ MonoBehaviour:
|
|||||||
m_Name: PlayerFollowerSettings
|
m_Name: PlayerFollowerSettings
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
moveSpeed: 25
|
moveSpeed: 25
|
||||||
|
moveAcceleration: 10000
|
||||||
stopDistance: 2
|
stopDistance: 2
|
||||||
useRigidbody: 1
|
useRigidbody: 1
|
||||||
defaultHoldMovementMode: 1
|
defaultHoldMovementMode: 1
|
||||||
|
|||||||
Reference in New Issue
Block a user