private void Update()
    {
        // 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;
    }
    private void Update()
    {
        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;
        }

        if (Vector3.Dot(progressDelta, progressPoint.direction) > 1)
        {
            progressDistance -= progressDelta.magnitude * 0.5f;
        }

        lastPosition = transform.position;

        raceCompletion = ((progressDistance / RaceManager.instance.raceDistance) * 100) / RaceManager.instance.totalLaps;
        raceCompletion = Mathf.Clamp(raceCompletion, -Mathf.Infinity, 100);
        raceCompletion = Mathf.Round(raceCompletion * 100) / 100;
    }
    void Update()
    {
        if (Track != null && target != null && pathExist)
        {
            target.position = Track.GetRoutePoint(progressDistance + 1.45f).position;                                                           // find the next position for the target
            target.rotation = Quaternion.LookRotation(Track.GetRoutePoint(progressDistance).direction);                                         // find the new rotation for the target


            progressPoint = Track.GetRoutePoint(progressDistance);                                                                                                              // --> Get the progressPoint position
            Vector3 progressDelta = progressPoint.position - transform.position;
            if (Vector3.Dot(progressDelta, progressPoint.direction) < 0)                                                                                                        // if progress point position is behind the car
            {
                progressDistance += progressDelta.magnitude * 0.5f;                                                                                                             // change the progress point position
            }
        }

        if (Track != null && progressDistance / Track.Length > 1)
        {
            //Debug.Log ("Lap");
            iLapCounter++;
            if (sLapCounter)
            {
                sLapCounter.displayLap(carPathFollow);
            }
            progressDistance = progressDistance % Track.Length;
        }
    }
Example #4
0
    private void Update()
    {
        if (circuit == null)
        {
            return;
        }
        if (progressStyle == ProgressStyle.SmoothAlongRoute)
        {
            // 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
        {
            // 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];
            //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;
        }
    }
Example #5
0
 void Update()
 {
     //计算距离
     dis += Time.deltaTime * speed;
     //获取相应距离在路径上的位置坐标
     transform.position = circuit.GetRoutePoint(dis).position;
     //获取相应距离在路径上的方向
     transform.rotation = Quaternion.LookRotation(circuit.GetRoutePoint(dis).direction);
 }
Example #6
0
 // Reset everything to the starting point.
 public void ResetWaypointCircuit()
 {
     progressDistance   = 0;
     transform.position = circuit.Waypoints[0].position;
     transform.LookAt(circuit.GetRoutePoint(lookAheadDistance + 6.576f).position);
     if (varyingSpeed)
     {
         currentSpeed = initialSpeed;
     }
     else
     {
         currentSpeed = routeSpeed;
     }
 }
Example #7
0
    // Update is called once per frame
    void Update()
    {
        Debug.Log("当前最后一个路径点坐标:" + circuit.Waypoints[circuit.Waypoints.Length - 1].position);
        if (transform.position == circuit.Waypoints[circuit.Waypoints.Length - 1].position)
        {
            return;
        }

        //计算距离
        dis += Time.deltaTime * speed;
        //获取相应距离在路径上的位置坐标
        transform.position = circuit.GetRoutePoint(dis).position;
        //获取相应距离在路径上的方向
        transform.rotation = Quaternion.LookRotation(circuit.GetRoutePoint(dis).direction);
    }
Example #8
0
    public float speed = 1f; // current speed of this object (calculated from delta since last frame)

    // setup script properties

    /// <summary>
    /// Start is called on the frame when a script is enabled just before
    /// any of the Update methods is called the first time.
    /// </summary>
    void Start()
    {
        distance = circuit.distances[circuit.startPoint];
        WaypointCircuit.RoutePoint startPoint = circuit.GetRoutePoint(distance);
        Vector3 position =
            startPoint
            .position;

        transform.rotation =
            Quaternion.LookRotation(
                startPoint
                .direction);
        position.y         = transform.position.y;
        transform.position = position;
    }
Example #9
0
    void Update()
    {
        Debug.Log(getFirstCarOnRace());
        foreach (CarController car in _carContainer.GetComponentsInChildren <CarController>(true))
        {
            float counter;
            counter = _carPositions[car].lap * 1000 + _carPositions[car].checkPoint * 100;
            if (car.GetComponent <WaypointProgressTracker>() != null)
            {
                WaypointProgressTracker wp = car.GetComponent <WaypointProgressTracker>();
                counter += wp.GetProgressDistance();
                _carPositions[car].progress = counter;
            }
            else
            {
                if (_carPositions[car].target == null)
                {
                    _carPositions[car].target = new GameObject(name + " Waypoint Target").transform;
                }

                Vector3 targetDelta = _carPositions[car].target.position - car.transform.position;
                if (targetDelta.magnitude < 20)
                {
                    _carPositions[car].progressNum = (_carPositions[car].progressNum + 1) % circuit.Waypoints.Length;
                }

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


                _carPositions[car].progressPoint = circuit.GetRoutePoint(_carPositions[car].progressDistance);
                Vector3 progressDelta = _carPositions[car].progressPoint.position - car.transform.position;
                if (Vector3.Dot(progressDelta, _carPositions[car].progressPoint.direction) < 0)
                {
                    _carPositions[car].progressDistance += progressDelta.magnitude;
                }
                counter += _carPositions[car].progressDistance;
                _carPositions[car].progress = +counter;
            }
        }
    }