[Player] Add AI Pathfinding on a 2D grid to the player character
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
using Pathfinding; // Add this at the top
|
||||
|
||||
// Basic touch/mouse movement controller suitable for top-down 2D or 3D overworld
|
||||
// Attach to the player GameObject. Works with or without Rigidbody/Rigidbody2D.
|
||||
@@ -9,8 +10,6 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
|
||||
{
|
||||
public float moveSpeed = 5f;
|
||||
public float stopDistance = 0.1f;
|
||||
|
||||
// If true and a Rigidbody/Rigidbody2D is present, movement will use physics MovePosition.
|
||||
public bool useRigidbody = true;
|
||||
|
||||
Vector3 targetPosition;
|
||||
@@ -18,11 +17,13 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
|
||||
|
||||
Rigidbody rb3d;
|
||||
Rigidbody2D rb2d;
|
||||
AIPath aiPath; // Reference to AIPath
|
||||
|
||||
void Awake()
|
||||
{
|
||||
rb3d = GetComponent<Rigidbody>();
|
||||
rb2d = GetComponent<Rigidbody2D>();
|
||||
aiPath = GetComponent<AIPath>(); // Get AIPath component
|
||||
}
|
||||
|
||||
void Start()
|
||||
@@ -58,58 +59,16 @@ public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
|
||||
Debug.Log($"PlayerTouchController.SetTargetPosition: worldPosition={worldPosition}");
|
||||
targetPosition = new Vector3(worldPosition.x, worldPosition.y, transform.position.z);
|
||||
hasTarget = true;
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (hasTarget)
|
||||
if (aiPath != null)
|
||||
{
|
||||
MoveTowardsTarget();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveTowardsTarget()
|
||||
{
|
||||
// Keep original y (or z for 2D) depending on scene setup. We assume the player moves on the plane defined by its current transform.
|
||||
Vector3 current = transform.position;
|
||||
|
||||
// Create a target that preserves the movement plane (so we don't change vertical axis unexpectedly)
|
||||
Vector3 planarTarget = targetPosition;
|
||||
|
||||
// If the camera is orthographic top-down (y is up), preserve y
|
||||
// Heuristic: if camera forward is (0,-1,0) then y is up and we should preserve y
|
||||
Camera cam = Camera.main;
|
||||
if (cam != null && Vector3.Dot(cam.transform.forward, Vector3.down) > 0.5f)
|
||||
{
|
||||
planarTarget.y = current.y;
|
||||
aiPath.destination = targetPosition;
|
||||
Debug.Log($"AIPath destination set to {targetPosition}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise assume z is depth for 2D setups and preserve z
|
||||
planarTarget.z = current.z;
|
||||
}
|
||||
|
||||
float step = moveSpeed * Time.deltaTime;
|
||||
Vector3 next = Vector3.MoveTowards(current, planarTarget, step);
|
||||
|
||||
// If using Rigidbody, move via physics when present
|
||||
if (useRigidbody && rb2d != null)
|
||||
{
|
||||
rb2d.MovePosition(new Vector2(next.x, next.y));
|
||||
transform.position = new Vector3(next.x, next.y, transform.position.z); // ensure transform matches
|
||||
}
|
||||
else if (useRigidbody && rb3d != null)
|
||||
{
|
||||
rb3d.MovePosition(next);
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.position = next;
|
||||
}
|
||||
|
||||
if (Vector3.Distance(transform.position, planarTarget) <= stopDistance)
|
||||
{
|
||||
hasTarget = false;
|
||||
Debug.LogWarning("AIPath component not found, falling back to direct movement");
|
||||
}
|
||||
}
|
||||
|
||||
// Remove FixedUpdate and MoveTowardsTarget, as AIPath handles movement
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user