using UnityEngine;
using RPG.Movement;
using TMPro;
using UnityEngine.AI;
namespace RPG.Control
{
public class PlayerController : MonoBehaviour
{
CharacterController ct;
NavMeshAgent nma;
Mover mover;
void Start()
{
ct = GetComponent<CharacterController>();
nma = GetComponent<NavMeshAgent>();
mover = GetComponent<Mover>();
}
// Update is called once per frame
void Update()
{
Movement();
if (Input.GetMouseButtonDown(0))
{
ClickToMove();
}
}
void ClickToMove()
{
ct.enabled= false;
nma.enabled = true;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool hasHit = Physics.Raycast(ray, out hit);
if (hasHit)
{
GetComponent<NavMeshAgent>().destination = hit.point;
}
}
void Movement()
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (input != Vector3.zero)
{
nma.enabled = false;
ct.enabled = true;
// Smoothly rotate the player towards the desired direction
Quaternion targetRotation = Quaternion.LookRotation(input, Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, mover.playerRotationSpeed * Time.deltaTime);
mover.Move(input);
//transform.forward = move;
}
}
}
}