Exemplo n.º 1
0
 private void FixedUpdate()
 {
     if (aiStarted && Vector3.Distance(transform.position, waypointsList[currentWaypoint].transform.position) < 50f)
     {
         currentWaypoint++;
         carAIControl.SetTarget(waypointsList[currentWaypoint].transform);
         //cube.transform.position = waypointsList[currentWaypoint].transform.position;
         //Debug.Log("SetNewWaypoint: " + waypointsList[currentWaypoint].transform.position);
     }
 }
    // Use this for initialization
    void Start()
    {
        user = GetComponent <CarUserControl>();
        ai   = GetComponent <CarAIControl>();
        ai.SetTarget(GameObject.Find("AI_Target").transform);

        control = GetComponent <CarController>();

        user.enabled = false;
        ai.enabled   = true;
    }
    private void FindPath()
    {
        //path.ClearCorners();
        //I think areamask 1 is 'walkable' in the navmesh bake menu, so i've set the parameter to 1.
        //print("FINDING PATH");
        NavMesh.CalculatePath(gameObject.transform.position, chaseTarget.transform.position, 1, path);

        pointCounter = 0;
        emptyTransformPrefab.transform.position = path.corners[pointCounter];
        carAI.SetTarget(emptyTransformPrefab.transform);

        needPath = false;
    }
    private void Update()
    {
        if (progressStyle == ProgressStyle.SmoothAlongRoute && circuitFound)
        {
            // determine the position we should currently be aiming for
            // (this is different to the current progress position, it is a a certain amount ahead along the route)
            // we use lerp as a simple way of smoothing out the speed over time.
            if (Time.deltaTime > 0)
            {
                speed = Mathf.Lerp(speed, (lastPosition - transform.position).magnitude / Time.deltaTime,
                                   Time.deltaTime);
            }

            target.position =
                circuit.GetRoutePoint(progressDistance + lookAheadForTargetOffset +
                                      lookAheadForTargetFactor * speed)
                .position;
            target.rotation =
                Quaternion.LookRotation(
                    circuit.GetRoutePoint(progressDistance + lookAheadForSpeedOffset +
                                          lookAheadForSpeedFactor * speed)
                    .direction);


            // get our current progress along the route
            progressPoint = circuit.GetRoutePoint(progressDistance);
            Vector3 progressDelta = progressPoint.position - transform.position;
            if (Vector3.Dot(progressDelta, progressPoint.direction) < 0)
            {
                progressDistance += progressDelta.magnitude * 0.5f;
            }

            lastPosition = transform.position;
        }
        else if (circuitFound)
        {
            // point to point mode. Just increase the waypoint if we're close enough:

            Vector3 targetDelta = target.position - transform.position;
            if (targetDelta.magnitude < pointToPointThreshold)
            {
                progressNum = (progressNum + 1) % circuit.Waypoints.Length;
            }


            target.position = circuit.Waypoints[progressNum].position;
            target.rotation = circuit.Waypoints[progressNum].rotation;

            // get our current progress along the route
            progressPoint = circuit.GetRoutePoint(progressDistance);
            Vector3 progressDelta = progressPoint.position - transform.position;
            if (Vector3.Dot(progressDelta, progressPoint.direction) < 0)
            {
                progressDistance += progressDelta.magnitude;
            }
            lastPosition = transform.position;
        }
        if (circuitFound)
        {
            _carAIControl.SetTarget(target);
        }
    }