示例#1
0
    public void Arrive()
    {
        if (!target)
        {
            return;
        }
        Vector3 direction = target.transform.position - transform.position;
        float   distance  = direction.magnitude;
        //1.确定速度
        float speed = 0;

        if (distance < targetRadius)
        {
            steering.ResetSteering();
            isArrived = true;
        }
        else if (distance > slowRadius)
        {
            speed = agent.maxSpeed;
        }
        else
        {
            speed = agent.maxSpeed * distance / slowRadius;
        }
        //2.指定理想速度
        Vector2 desiredVelocity = direction;

        desiredVelocity.Normalize();
        desiredVelocity *= speed;

        //3.计算实际速度
        steering.accel  = desiredVelocity - rigidbody2D.velocity;
        steering.accel /= timeToTarget;
        steering.accel  = Vector2.ClampMagnitude(steering.accel, agent.maxAccel);
    }