Ejemplo n.º 1
0
    void FixedUpdate()
    {
        //Get constant inputs
        if (!string.IsNullOrEmpty(accelAxis))
        {
            vp.SetAccel(Input.GetAxis(accelAxis));
        }

        if (!string.IsNullOrEmpty(brakeAxis))
        {
            vp.SetBrake(Input.GetAxis(brakeAxis));
        }

        if (!string.IsNullOrEmpty(steerAxis))
        {
            vp.SetSteer(Input.GetAxis(steerAxis));
        }

        if (!string.IsNullOrEmpty(ebrakeAxis))
        {
            vp.SetEbrake(Input.GetAxis(ebrakeAxis));
        }

        if (!string.IsNullOrEmpty(boostButton))
        {
            vp.SetBoost(Input.GetButton(boostButton));
        }

        if (!string.IsNullOrEmpty(pitchAxis))
        {
            vp.SetPitch(Input.GetAxis(pitchAxis));
        }

        if (!string.IsNullOrEmpty(yawAxis))
        {
            vp.SetYaw(Input.GetAxis(yawAxis));
        }

        if (!string.IsNullOrEmpty(rollAxis))
        {
            vp.SetRoll(Input.GetAxis(rollAxis));
        }

        if (!string.IsNullOrEmpty(upshiftButton))
        {
            vp.SetUpshift(Input.GetAxis(upshiftButton));
        }

        if (!string.IsNullOrEmpty(downshiftButton))
        {
            vp.SetDownshift(Input.GetAxis(downshiftButton));
        }
    }
Ejemplo n.º 2
0
    void FixedUpdate()
    {
        if (setter)
        {
            accelerationDelta = Input.acceleration - accelerationPrev;
            accelerationPrev  = Input.acceleration;
            vp.SetAccel(setter.accel);
            vp.SetBrake(setter.brake);
            vp.SetEbrake(setter.ebrake);
            vp.SetBoost(setter.boost);

            if (useAccelerometer)
            {
                vp.SetSteer((Input.acceleration.x + accelerationDelta.x * deltaFactor) * steerFactor);
                vp.SetYaw(Input.acceleration.x * flipFactor);
                vp.SetPitch(-Input.acceleration.z * flipFactor);
            }
            else
            {
                vp.SetSteer(setter.steer);
            }
        }
    }
Ejemplo n.º 3
0
    void FixedUpdate()
    {
        if (target)
        {
            if (target != targetPrev)
            {
                InitializeTarget();
            }

            targetPrev = target;

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

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

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

                    if (brakeTime <= 1)
                    {
                        brakeTime = 0;
                    }
                }
            }
            else
            {
                speed = initialSpeed;
            }

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

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

            if ((stoppedTime > stopTimeReverse || (vp.velMag < 1 && lookDot < 0) || (vp.velMag <= 20 && lookDot < -0.8)) && reverseTime == 0)
            {
                reverseTime = reverseAttemptTime;
                reverseAttempts++;
            }

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

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

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

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

        //Reset if stuck rolled over
        if (rolledOverTime > rollResetTime && rollResetTime >= 0)
        {
            StartCoroutine(ResetRotation());
        }
    }
Ejemplo n.º 4
0
    void Update()
    {
        //Calculating controll values for this frame...

        float speed = 3.6f * vp.velMag;

        targetSpeed = desiredSpeed;
        if (true)
        {
            SteerTowards(out targetSteer, MoveTarget);
            Vector3    rayCastOrigin             = (transform.position + transform.forward * 2 + transform.up * 1);
            float      DistanceMid               = 0;
            float      SpeedMid                  = 0;
            float      relativeVelocityAngle     = 0;
            float      speedToDistanceTollerance = 0.1f;
            RaycastHit HitMid;
            float      angle = 0;
            float[]    viewAngelsStraight = { 10, -10, 15, 30, 45, 60 };
            bool       HitSuccess         = LookForward(rayCastOrigin, 0, out DistanceMid, out SpeedMid, out relativeVelocityAngle, out HitMid, true);
            int        i = 0;
            while (!HitSuccess)
            {
                ///Debug.Log("We should stop maybe i: "+i +"  Length:"+viewAngelsStraight.Length);
                if (viewAngelsStraight.Length <= i)
                {
                    break;
                }
                if (targetSteer < 0)
                {
                    HitSuccess = LookForward(rayCastOrigin, -viewAngelsStraight[i], out DistanceMid, out SpeedMid, out relativeVelocityAngle, out HitMid, true);
                }
                else if (targetSteer > 0)
                {
                    HitSuccess = LookForward(rayCastOrigin, viewAngelsStraight[i], out DistanceMid, out SpeedMid, out relativeVelocityAngle, out HitMid, true);
                }
                else if (targetSteer == 0)
                {
                    HitSuccess = LookForward(rayCastOrigin, viewAngelsStraight[i], out DistanceMid, out SpeedMid, out relativeVelocityAngle, out HitMid, true);
                    if (!HitSuccess)
                    {
                        HitSuccess = LookForward(rayCastOrigin, -viewAngelsStraight[i], out DistanceMid, out SpeedMid, out relativeVelocityAngle, out HitMid, true);
                    }
                }
                i++;
            }
            if (HitSuccess && false) /// hello new me   how areyou ...listen, this is really npot a good way to code, i know but i did not want anything messing with targetSpeed .. cheers all the best... David
            {
                if (HitMid.transform.GetComponent <Rigidbody>() != null)
                {
                    if (relativeVelocityAngle < 15f)
                    {
                        //if thats almost the same lets say within 15 degree Absolute we adapt the speed
                        if (DistanceMid > SpeedMid + SpeedMid * speedToDistanceTollerance)
                        {
                            targetSpeed = Mathf.Clamp(scale(SpeedMid + SpeedMid * speedToDistanceTollerance, 50, SpeedMid, 120, DistanceMid), 0, 120);
                        }
                        else if (DistanceMid < SpeedMid - SpeedMid * speedToDistanceTollerance)
                        {
                            targetSpeed = Mathf.Clamp(scale(0, SpeedMid - SpeedMid * speedToDistanceTollerance, 0, SpeedMid, DistanceMid), 0, 120);
                        }
                        else
                        {
                            targetSpeed = SpeedMid;
                        }
                    }
                    else if (relativeVelocityAngle >= 15f && relativeVelocityAngle < 90f)
                    {//if not we decide to either stop or reduce speed
                        if (DistanceMid > SpeedMid + SpeedMid * speedToDistanceTollerance)
                        {
                            targetSpeed = Mathf.Clamp(scale(SpeedMid + SpeedMid * speedToDistanceTollerance, 50, SpeedMid, 120, DistanceMid), 0, 120);
                        }
                        else if (DistanceMid < SpeedMid - SpeedMid * speedToDistanceTollerance)
                        {
                            targetSpeed = Mathf.Clamp(scale(0, SpeedMid - SpeedMid * speedToDistanceTollerance, 0, SpeedMid, DistanceMid), 0, 120);
                        }
                        else
                        {
                            targetSpeed = SpeedMid;
                        }
                        targetSpeed *= 0.9f;
                    }
                    else
                    {
                        if (HitSuccess && DistanceMid < 5)
                        {
                            //full_stop = true;  /// EDIT call the car stoped function
                            targetSpeed = 0;
                        }
                        else
                        {
                            full_stop   = false;
                            targetSpeed = 15f;
                        }
                    }
                }
                //Debug.Log ("TargetCarAngle: "+target_car_angle+"TargetSpeed: " + targetSpeed + "Distance: " + hit.distance+"minDistnace: "+(target_car_speed - target_car_speed * speedToDistanceTollerance).ToString()+"MaxDistance:"+(target_car_speed + target_car_speed * speedToDistanceTollerance).ToString());
                if (HitSuccess && DistanceMid <= 4)
                {
                    // here we just start driving agiain
                    //full_stop = true;   /// EDIT call the car stoped function
                }
                else if (HitSuccess && DistanceMid > 4 && DistanceMid <= 20)
                {
                    targetSpeed = desiredSpeed;
                    // this should be filled with code if we get a rigid body as a raycast target
                    // that is not a a rigid body i.e. city or so... important
                }
                else if (HitSuccess && DistanceMid > 20)
                {
                    targetSpeed = desiredSpeed;
                    // this should be filled with code if we get a rigid body as a raycast target
                    // that is not a a rigid body i.e. city or so... important
                }
            }
            ///Here we Cap the speed to something that will assure we will not crash
            ///
            if (targetSpeed > desiredSpeed + 10)
            {
                targetSpeed = desiredSpeed + 10;
            }
        }
        else
        {
            //define new target speed and steering based on in put from the
        }
        //// accalerator calculation
        if (!full_stop)
        {
            // Debug.Log(targetSpeed);
            vp.SetEbrake(0);
            float temp = speedController.Update(targetSpeed, speed, Time.deltaTime);
            // Debug.Log("We are in the PID section its telling us a throttle of : " + temp);
            if (temp > 0)
            {
                if (targetSpeed > 0)
                {
                    vp.SetAccel(Mathf.Clamp01(temp));
                    vp.SetBrake(0);
                }
            }
            else if (temp < 0.1f)
            {
                vp.SetAccel(0);

                if (targetSpeed == 0)
                {
                    vp.SetBrake(Mathf.Clamp01(-1 * temp) * 3);
                }
                else
                {
                    vp.SetBrake(Mathf.Clamp01(-1 * temp) * 1.75f);
                }
            }
        }
        else
        {
            vp.SetEbrake(1);
            vp.SetAccel(0);
            vp.SetBrake(0.23f);
        }
        vp.SetSteer(Mathf.MoveTowards(vp.steerInput, targetSteer, steerRate * Time.deltaTime));
    }