嗨,我正在尝试通过单击鼠标来移动我的角色,现在一切似乎都很好,角色正在移动并且动画正在运行。但是每当角色到达目的地点时,角色就会一直走到目的地点旁边并无休止地绕圈。 我该如何解决?
using UnityEngine;
using UnityEngine.AI;
public class CharacterMoveScript : MonoBehaviour
{
public Camera cam;
public NavMeshAgent player;
public GameObject targetDest;
public Animator playerAnimator;
private string groundTag = "Ground";
private RaycastHit hit;
private bool isMoving = false;
void Start()
{
player = GetComponent<NavMeshAgent>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.collider.CompareTag(groundTag))
{
player.SetDestination(hit.point);
isMoving = true;
}
}
}
UpdateAnimation();
if (isMoving)
{
// Check if the character has reached the destination
if (!player.pathPending && player.remainingDistance <= player.stoppingDistance)
{
if (!player.hasPath || player.velocity.sqrMagnitude == 0f)
{
isMoving = false;
player.isStopped = true;
}
}
}
}
private void UpdateAnimation()
{
playerAnimator.SetBool("isWalking", isMoving && player.velocity.magnitude > 0.1f);
UnityEngine.Debug.Log("is moving" + isMoving);
UnityEngine.Debug.Log("is stoped" + player.isStopped);
UnityEngine.Debug.Log(player.pathStatus==NavMeshPathStatus.PathComplete);
}
}
当游戏开始ismoving时,istoped = false,当我点击一个目的地时isMoving变为true并且到达目的地时不停止