示例#1
0
    public void MoveToTarget()
    {
        if (error_.enabled && error_.GetDistracted())
        {
            //check if the body is stopped (account for slight drift)
            if (body_.velocity.magnitude < 0.01f)
            {
                return;
            }
        }

        //apply the fine movement modifier
        bool  fineMovement       = state_ == CarState.CS_PARKING || state_ == CarState.CS_DEPARKING;
        float distanceMultiplier = fineMovement ? fineMovementMultiplier_ : 1.0f;

        if (DistanceToTarget() > arrivalDistance_)// * distanceMultiplier)
        {
            //make sure the car doesn't exceed the maximum speed
            if (body_.velocity.magnitude < maxSpeed_ * distanceMultiplier)
            {
                float steerAngle = Mathf.Abs(transform.FindChild("wheel_FL").GetComponent <WheelCollider>().steerAngle);
                float direction  = 1.0f;
                if (Reversing())
                {
                    direction *= -1.0f;
                }
                Accelerate(Mathf.Min(1 / (steerAngle / turnSpeed_), 2.5f) * direction);
            }
            else
            {
                Accelerate(0);
            }
        }
        else
        {
            Brake();
            //if slowed enough, find a new target
            if (body_.velocity.magnitude < maxSpeed_ * 0.75f * distanceMultiplier)
            {
                //make sure there are target
                if (targets_.Count == 0)
                {
                    if (state_ == CarState.CS_PARKING)
                    {
                        state_ = CarState.CS_PARKED;
                        return;
                    }
                    else if (state_ == CarState.CS_DEPARKING)
                    {
                        state_ = CarState.CS_MOVING;
                        targets_.Enqueue(curRoad_.GetComponent <Road>().GetEnd().position);
                    }
                    else if (state_ != CarState.CS_PARKED)
                    {
                        GetTargets();
                    }
                }
                else if (purpose_.enabled && state_ != CarState.CS_PARKING && state_ != CarState.CS_DEPARKING)
                {
                    purpose_.TestPark();
                }
                //double check that there are available targets
                if (targets_.Count > 0)
                {
                    //get a new target from the queue
                    target_ = targets_.Dequeue();
                }
            }
        }
    }