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;
    }
Exemplo n.º 3
0
    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;
        }
    }
Exemplo n.º 4
0
 private void Update()
 {
     if (this.progressStyle == WaypointProgressTracker.ProgressStyle.SmoothAlongRoute)
     {
         if (Time.deltaTime > 0f)
         {
             this.speed = Mathf.Lerp(this.speed, (this.lastPosition - base.transform.position).magnitude / Time.deltaTime, Time.deltaTime);
         }
         this.target.position = this.circuit.GetRoutePoint(this.progressDistance + this.lookAheadForTargetOffset + this.lookAheadForTargetFactor * this.speed).position;
         this.target.rotation = Quaternion.LookRotation(this.circuit.GetRoutePoint(this.progressDistance + this.lookAheadForSpeedOffset + this.lookAheadForSpeedFactor * this.speed).direction);
         this.progressPoint   = this.circuit.GetRoutePoint(this.progressDistance);
         Vector3 lhs = this.progressPoint.position - base.transform.position;
         if (Vector3.Dot(lhs, this.progressPoint.direction) < 0f)
         {
             this.progressDistance += lhs.magnitude * 0.5f;
         }
         this.lastPosition = base.transform.position;
     }
     else
     {
         if ((this.target.position - base.transform.position).magnitude < this.pointToPointThreshold)
         {
             this.progressNum = (this.progressNum + 1) % this.circuit.Waypoints.Length;
         }
         this.target.position = this.circuit.Waypoints[this.progressNum].position;
         this.target.rotation = this.circuit.Waypoints[this.progressNum].rotation;
         this.progressPoint   = this.circuit.GetRoutePoint(this.progressDistance);
         Vector3 lhs2 = this.progressPoint.position - base.transform.position;
         if (Vector3.Dot(lhs2, this.progressPoint.direction) < 0f)
         {
             this.progressDistance += lhs2.magnitude;
         }
         this.lastPosition = base.transform.position;
     }
 }
Exemplo n.º 5
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;
        }
    }
Exemplo n.º 6
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;
    }
Exemplo n.º 7
0
 private void FixedUpdate()
 {
     if (isMoving && canMove)
     {
         if (distance > circuit.distances[circuit.stopPoint])
         {
             distance = circuit.distances[circuit.startPoint];
             return;
         }
         WaypointCircuit.RoutePoint point = circuit.GetRoutePoint(distance);
         Vector3 position = point.position;
         transform.rotation = Quaternion.LookRotation(point.direction);
         position.y         = transform.position.y;
         transform.position = position;
         distance          += speed * Time.deltaTime;
     }
 }
Exemplo n.º 8
0
    /// <summary>
    /// Function called each frame
    /// </summary>
    private void Update()
    {
        // if we are using a WaypointCircuit we recalculate the position of the objects used to navigate it
        if (waypoint.isCircuit())
        {
            if (actualTimer >= 0)
            {
                actualTimer -= Time.deltaTime;
            }
            else
            {
                actualTimer += timer;

                // calculating test-Points in circuit
                int       nOfPoints = (int)lookAheadForTargetOffset;
                Vector3[] pBetween  = waypoint.pointsBetween(getRoutePosition(), target.transform.position, nOfPoints);

                // calculating the matching points in the line between the drone and the target
                Vector3[] destPoints = new Vector3[pBetween.Length];
                for (int i = 0; i < pBetween.Length; i++)
                {
                    destPoints[i] = getPerpendicolarPoint(transform.position, target.position, pBetween[i]);
                }

                // calculating sum of distances between the test point and it's matching point
                overallDistanceFromTrajectory = 0;
                for (int i = 0; i < pBetween.Length - 1; i++)
                {
                    overallDistanceFromTrajectory += Vector3.Distance(pBetween[i], destPoints[i]);
                }

                // so we increase o decrease the distance of the target
                if (overallDistanceFromTrajectory > maxDistFromCircuit)
                {
                    lookAheadForTargetOffset -= 0.25f;
                }
                else
                {
                    lookAheadForTargetOffset += 0.25f;
                }


                lookAheadForTargetOffset = droneSettings.keepOnRange(lookAheadForTargetOffset, 3f, 12f);
            }

            // determine the position we should currently be aiming for
            // (this is different to the current progress position, it is a certain amount ahead along the route)
            target.position = waypoint.GetRoutePoint(progressDistance + lookAheadForTargetOffset).position;
            target.rotation = Quaternion.LookRotation(waypoint.GetRoutePoint(progressDistance).direction);
            // get our current progress along the route

            needToRecalculateDistanceFromCircuit = Vector3.Distance(transform.position, getRoutePosition()) > 10 ? true : needToRecalculateDistanceFromCircuit;
            if (needToRecalculateDistanceFromCircuit)
            {
                progressDistance = waypoint.getNearestPointTo(transform.position);
                needToRecalculateDistanceFromCircuit = false;
            }
            else
            {
                WaypointCircuit.RoutePoint progressPoint = waypoint.GetRoutePoint(progressDistance);
                Vector3 progressDelta = progressPoint.position - transform.position;
                if (Vector3.Dot(progressDelta, progressPoint.direction) < 0)
                {
                    progressDistance += progressDelta.magnitude * 0.5f;
                }
            }

            // setting drones variables
            gameObject.GetComponent <droneMovementController>().setRoutePos(getRoutePosition());
            float distToRoute = Vector3.Distance(transform.position, getRoutePosition());
            gameObject.GetComponent <droneMovementController>().setLookingPoint(waypoint.GetRoutePosition(progressDistance + distanceOfPointToLookAt - distToRoute));
            gameObject.GetComponent <droneMovementController>().stayOnFixedPoint = false;
        }
        else
        {
            Vector3 routePos = getRoutePosition();

            // setting drones variables
            gameObject.GetComponent <droneMovementController>().setRoutePos(routePos);
            target.position = getRoutePosition();// + Vector3.forward;
            gameObject.GetComponent <droneMovementController>().stayOnFixedPoint = true;
            gameObject.GetComponent <droneMovementController>().setLookingPoint(((singlePoint)waypoint).getLookingAtPoint());
        }
    }
	void Update()
	{
		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 ].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;
		}

	}