Files
AppleHillsProduction/Assets/Scripts/PlayerTouchController.cs

97 lines
2.9 KiB
C#

using UnityEngine;
#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.
public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
{
public float moveSpeed = 5f;
public float stopDistance = 0.1f;
public bool useRigidbody = true;
Vector3 targetPosition;
bool hasTarget = false;
Rigidbody rb3d;
Rigidbody2D rb2d;
AIPath aiPath; // Reference to AIPath
private Animator animator;
private Transform artTransform;
void Awake()
{
rb3d = GetComponent<Rigidbody>();
rb2d = GetComponent<Rigidbody2D>();
aiPath = GetComponent<AIPath>(); // Get AIPath component
// Find art prefab and animator
artTransform = transform.Find("CharacterArt");
if (artTransform != null)
{
animator = artTransform.GetComponent<Animator>();
}
else
{
animator = GetComponentInChildren<Animator>(); // fallback
}
}
void Start()
{
// Initialize target to current position so object doesn't snap
targetPosition = transform.position;
hasTarget = false;
// Register as default consumer in Start, after InputManager is likely initialized
InputManager.Instance?.SetDefaultConsumer(this);
}
void OnEnable()
{
// No longer register here
}
// Remove Update and HandleInput
public void OnTouchPress(Vector2 worldPosition)
{
Debug.Log($"PlayerTouchController.OnTouchPress received worldPosition: {worldPosition}");
SetTargetPosition(worldPosition);
}
public void OnTouchPosition(Vector2 screenPosition)
{
Debug.Log($"PlayerTouchController.OnTouchPosition called with screenPosition: {screenPosition}");
// Optionally handle drag/move here
}
void SetTargetPosition(Vector2 worldPosition)
{
Debug.Log($"PlayerTouchController.SetTargetPosition: worldPosition={worldPosition}");
targetPosition = new Vector3(worldPosition.x, worldPosition.y, transform.position.z);
hasTarget = true;
if (aiPath != null)
{
aiPath.destination = targetPosition;
Debug.Log($"AIPath destination set to {targetPosition}");
}
else
{
Debug.LogWarning("AIPath component not found, falling back to direct movement");
}
}
void Update()
{
// Update animator speed parameter
if (animator != null && aiPath != null)
{
float normalizedSpeed = aiPath.velocity.magnitude / aiPath.maxSpeed;
animator.SetFloat("Speed", Mathf.Clamp01(normalizedSpeed));
}
}
// Remove FixedUpdate and MoveTowardsTarget, as AIPath handles movement
}