Esempio n. 1
0
        public void InitializeTarget()
        {
            if (this.target)
            {
                //if target is a vehicle
                this.targetBody = (Rigidbody)F.GetTopmostParentComponent <Rigidbody>(this.target);

                //if target is a waypoint
                this.targetWaypoint = this.target.GetComponent <VehicleWaypoint>();
                if (this.targetWaypoint)
                {
                    this.prevSpeed = this.targetWaypoint.speed;
                }
            }
        }
Esempio n. 2
0
        void FixedUpdate()
        {
            if (this.target)
            {
                if (this.target != this.targetPrev)
                {
                    this.InitializeTarget();
                }

                this.targetPrev = this.target;

                //Is the target a waypoint?
                this.targetIsWaypoint = this.target.GetComponent <VehicleWaypoint>();
                //Can I see the target?
                this.targetVisible = !Physics.Linecast(this.tr.position, this.target.position, this.viewBlockMask);

                if (this.targetVisible || this.targetIsWaypoint)
                {
                    this.targetPoint = this.targetBody
                                 ? this.target.position + this.targetBody.velocity
                                 : this.target.position;
                }

                if (this.targetIsWaypoint)
                {
                    //if vehicle is close enough to target waypoint, switch to the next one
                    if ((this.tr.position - this.target.position).sqrMagnitude
                        <= this.targetWaypoint.radius * this.targetWaypoint.radius)
                    {
                        this.target         = this.targetWaypoint.nextPoint.transform;
                        this.targetWaypoint = this.targetWaypoint.nextPoint;
                        this.prevSpeed      = this.speed;
                        this.speed          = Mathf.Clamp01(this.targetWaypoint.speed * this.initialSpeed);
                        this.brakeTime      = this.prevSpeed / this.speed;

                        if (this.brakeTime <= 1)
                        {
                            this.brakeTime = 0;
                        }
                    }
                }

                this.brakeTime = Mathf.Max(0, this.brakeTime - Time.fixedDeltaTime);
                //Is the distance to the target less than the follow distance?
                this.close =
                    (this.tr.position - this.target.position).sqrMagnitude <= Mathf.Pow(this.followDistance, 2) &&
                    !this.targetIsWaypoint;
                this.dirToTarget = (this.targetPoint - this.tr.position).normalized;
                this.lookDot     = Vector3.Dot(this.vp.forwardDir, this.dirToTarget);
                this.steerDot    = Vector3.Dot(this.vp.rightDir, this.dirToTarget);

                //Attempt to reverse if vehicle is stuck
                this.stoppedTime = Mathf.Abs(this.vp.localVelocity.z) < 1 && !this.close && this.vp.groundedWheels > 0
                               ? this.stoppedTime + Time.fixedDeltaTime
                               : 0;

                if (this.stoppedTime > this.stopTimeReverse && this.reverseTime == 0)
                {
                    this.reverseTime = this.reverseAttemptTime;
                    this.reverseAttempts++;
                }

                //Reset if reversed too many times
                if (this.reverseAttempts > this.resetReverseCount && this.resetReverseCount >= 0)
                {
                    this.StartCoroutine(this.ReverseReset());
                }

                this.reverseTime = Mathf.Max(0, this.reverseTime - Time.fixedDeltaTime);

                if (this.targetVelocity > 0)
                {
                    this.speedLimit = Mathf.Clamp01(this.targetVelocity - this.vp.localVelocity.z);
                }
                else
                {
                    this.speedLimit = 1;
                }

                //Set vehicle inputs
                this.vp.SetAccel(!this.close &&
                                 (this.lookDot > 0 || this.vp.localVelocity.z < 5) &&
                                 this.vp.groundedWheels > 0 &&
                                 this.reverseTime == 0
                             ? this.speed * this.speedLimit
                             : 0);
                this.vp.SetBrake(this.reverseTime == 0 &&
                                 this.brakeTime == 0 &&
                                 !(this.close && this.vp.localVelocity.z > 0.1f)
                             ? (this.lookDot < 0.5f && this.lookDot > 0 && this.vp.localVelocity.z > 10
                                    ? 0.5f - this.lookDot
                                    : 0)
                             : (this.reverseTime > 0
                                    ? 1
                                    : (this.brakeTime > 0
                                           ? this.brakeTime * 0.2f
                                           : 1
                                       - Mathf.Clamp01(Vector3.Distance(this.tr.position,
                                                                        this.target.position)
                                                       / Mathf.Max(0.01f, this.followDistance)))));
                this.vp.SetSteer(this.reverseTime == 0
                             ? Mathf.Abs(Mathf.Pow(this.steerDot,
                                                   (this.tr.position - this.target.position).sqrMagnitude > 20
                                                       ? 1
                                                       : 2))
                                 * Mathf.Sign(this.steerDot)
                             : -Mathf.Sign(this.steerDot) * (this.close ? 0 : 1));
                this.vp.SetEbrake((this.close && this.vp.localVelocity.z <= 0.1f) ||
                                  (this.lookDot <= 0 && this.vp.velMag > 20)
                              ? 1
                              : 0);
            }

            this.rolledOverTime = this.va.rolledOver ? this.rolledOverTime + Time.fixedDeltaTime : 0;

            //Reset if stuck rolled over
            if (this.rolledOverTime > this.rollResetTime && this.rollResetTime >= 0)
            {
                this.StartCoroutine(this.ResetRotation());
            }
        }