Ejemplo n.º 1
0
    private void JoinLaneIfSafe(Vector3 direction)
    {
        // Debug.Log("Current speed is " + vehicle.GetCurrentSpeed() + ", ideal speed " + idealLaneMergingSpeed.ToString());
        if (ReachedDestinationLane())
        {
            // Debug.Log("Reached destination lane");
            CurrentLane  = GetDesiredLane();
            CurrentState = DrivingAgentState.Driving;
            return;
        }

        if (IsDesiredLaneFreeOfObstacles())
        {
            Debug.Log("Lane is free. Steering towards it...");

            Vector3 dir = (GetLaneAdjustedTarget() - gameObject.transform.position).normalized;
            float   currentSteeringAngle = Vector3.Dot(dir, transform.forward);

            // Steer towards lane.
            direction = TrafficDirection == Vector3.left ? direction : -direction;
            if (direction == Vector3.left && currentSteeringAngle > 1 - LaneMergingMaxSteeringAngle)
            {
                vehicle.SteerLeft();
            }

            else if (direction == Vector3.right && currentSteeringAngle > 1 - LaneMergingMaxSteeringAngle)
            {
                vehicle.SteerRight();
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        VehicleAgent vehicle = gameObject.GetComponent <VehicleAgent> ();

        if (Input.GetKey(forwardKey))
        {
            vehicle.Accelerate();
        }

        if (Input.GetKey(backwardKey))
        {
            vehicle.Brake();
        }

        if (Input.GetKey(leftKey))
        {
            vehicle.SteerLeft();
        }

        if (Input.GetKey(rightKey))
        {
            vehicle.SteerRight();
        }
    }