Initial commit
This commit is contained in:
40
Assets/Scripts/SimplePlayerController.cs
Normal file
40
Assets/Scripts/SimplePlayerController.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user