void Move(Vector3 myPos,Vector3 targetPos)
{
if (Time.time > _lastRepathTime + repathRate && seeker.IsDone())
{
_lastRepathTime = Time.time;
seeker.StartPath(myPos, targetPos, OnPathComplete);
}
if(path == null) return;
reachedEndOfPath = false;
//与当前路点之间的距离
float distaneToWaypoint;
while (true)
{
distaneToWaypoint = Vector3.Distance(myPos, path.vectorPath[currentWayPoint]);
//角色与当前路点的距离小于设定值,就会向下一个路点移动
if (distaneToWaypoint < pickNextWaypointDistance)
{
//是否到达寻路终点
if (currentWayPoint + 1 < path.vectorPath.Count)
{
currentWayPoint++;
}
else
{
reachedEndOfPath = true;
break;
}
}
else
{
break;
}
}
var moveDir = (path.vectorPath[currentWayPoint] - myPos).normalized;
var myTransform = transform;
myTransform.position = myPos + moveDir * (moveSpeed * Time.deltaTime);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(moveDir), rotateSpeed);
}
HI!
The above is the script to control the movement of the character
I use WASD keys to control the movement of characters
Most of the cases can work normally
However, if you move to the edge of the map or a huge obstacle, if you continue to hold the arrow keys, the character will start to shake wildly.
How do I know that the destination may be unreachable
So that i can stop the character from moving