Files
Click-PointRPG/Assets/Scripts/SimplePlayerController.cs

41 lines
1.0 KiB
C#
Raw Normal View History

2026-02-10 21:27:46 +00:00
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class SimplePlayerController : MonoBehaviour
{
public float rotationSpeed = 10f;
public float stopDistanceTolerance = 0.1f;
NavMeshAgent agent;
Vector3 lastVelocity;
void Awake()
{
agent = GetComponent<NavMeshAgent>();
agent.updateRotation = false; // manual rotation
}
void Update()
{
Vector3 vel = agent.velocity;
if (vel.sqrMagnitude > 0.01f)
{
Quaternion targetRot = Quaternion.LookRotation(vel.normalized);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotationSpeed * Time.deltaTime);
}
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance + stopDistanceTolerance)
{
agent.isStopped = true;
}
else
{
agent.isStopped = false;
}
lastVelocity = vel;
}
public Vector3 GetVelocity() => lastVelocity;
}