Support Forum

The character walks to the edge of the map and starts to shake wildly

    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

Hi

Sorry for the late reply.

Your movement script has a moveDir which is a normalized direction. If the agent is very close to the end of the path the direction to the next point will be veeery small. If you normalize it while the agent moves around slightly it may change direction and point in the completely opposite way. This will lead to shaking. The built-in movement scripts reduce shaking by disabling rotation if the agent is very close to the waypoint.