[Player][Interactions] Pulver moves to item and goes back. Item disappears, but in wrong order
This commit is contained in:
@@ -21,6 +21,11 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
|
||||
private Animator animator;
|
||||
private Transform artTransform;
|
||||
|
||||
public delegate void ArrivedAtTargetHandler();
|
||||
public event ArrivedAtTargetHandler OnArrivedAtTarget;
|
||||
private Coroutine moveToCoroutine;
|
||||
private bool interruptMoveTo = false;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
rb3d = GetComponent<Rigidbody>();
|
||||
@@ -56,6 +61,8 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
|
||||
|
||||
public void OnTouchPress(Vector2 worldPosition)
|
||||
{
|
||||
// If moving to pickup, interrupt
|
||||
InterruptMoveTo();
|
||||
Debug.Log($"PlayerTouchController.OnTouchPress received worldPosition: {worldPosition}");
|
||||
SetTargetPosition(worldPosition);
|
||||
}
|
||||
@@ -93,4 +100,48 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
|
||||
}
|
||||
|
||||
// Remove FixedUpdate and MoveTowardsTarget, as AIPath handles movement
|
||||
|
||||
// Move to a target position, notify when arrived
|
||||
public void MoveToAndNotify(Vector3 target)
|
||||
{
|
||||
if (moveToCoroutine != null)
|
||||
{
|
||||
StopCoroutine(moveToCoroutine);
|
||||
}
|
||||
interruptMoveTo = false;
|
||||
moveToCoroutine = StartCoroutine(MoveToTargetCoroutine(target));
|
||||
}
|
||||
|
||||
public void InterruptMoveTo()
|
||||
{
|
||||
interruptMoveTo = true;
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator MoveToTargetCoroutine(Vector3 target)
|
||||
{
|
||||
hasTarget = true;
|
||||
targetPosition = target;
|
||||
if (aiPath != null)
|
||||
{
|
||||
aiPath.destination = target;
|
||||
}
|
||||
while (!interruptMoveTo)
|
||||
{
|
||||
// 2D distance calculation (ignore z)
|
||||
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 <= stopDistance + 0.2f)
|
||||
{
|
||||
break;
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
hasTarget = false;
|
||||
moveToCoroutine = null;
|
||||
if (!interruptMoveTo)
|
||||
{
|
||||
OnArrivedAtTarget?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user