Пример #1
0
    void Update()
    {
        if (Gps != null)
        {
            if (_gpsTimer == 0)
            {
                Gps.UpdateSegIdx();
                _gpsTimer = 2;
            }
            _gpsTimer--;
        }

        //Engine Sound

        if (_prevEngineTorque > 0 && WCFL.motorTorque == 0)
        {
            EngineAudioSource.Stop();
            ClutchAudioSource.Play();
            ClutchStartTime = Time.time;
        }
        if (_prevEngineTorque == 0 && WCFL.motorTorque == 0 && IdleAudioSource.isPlaying == false && Time.time > ClutchStartTime + 1f && EngineAudioSource.isPlaying == false)
        {
            EngineAudioSource.pitch = 0.2f;
            EngineAudioSource.Play();
        }
        if (WCFL.motorTorque > 0)
        {
            ClutchAudioSource.Stop();
            //IdleAudioSource.Stop();
            float _pitch;
            float rpm;
            //int Gear;
            float Wheelrpm = Mathf.Abs((WCFL.rpm < WCFR.rpm) ? WCFL.rpm : WCFR.rpm);
            if (Wheelrpm > 1499)
            {
                Wheelrpm = 1499;
            }
            if (EngineAudioSource.isPlaying == false)
            {
                EngineAudioSource.Play();
            }
            if (Wheelrpm < 300)
            {
                rpm    = -Wheelrpm;
                _pitch = rpm / 600 - 0.5f;
                //Gear = 1;
            }
            else
            {
                rpm = -(Wheelrpm - 400) % 400;
                //Gear = (int)Wheelrpm / 400 + 1;
                _pitch = (rpm / 600 - 0.5f);
            }
            EngineAudioSource.pitch = _pitch * 1.5f;
        }
        CoughAudioSource.mute = (WCFL.motorTorque != 0);
        _prevEngineTorque     = WCFL.motorTorque;
    }
Пример #2
0
    void Update()
    {
        if (Gps != null)
        {
            if (_gpsTimer == 0)
            {
                Gps.UpdateSegIdx();
                _gpsTimer = 2;
            }
            _gpsTimer--;
        }

        //Engine Sound
        if (WCRL.motorTorque > 0 && WCRL.brakeTorque == 0)
        {
            if (IdleAudioSource.isPlaying || DecelAudioSource.isPlaying)
            {
                IdleAudioSource.Stop(); DecelAudioSource.Stop();
                RevAudioSource.Play(); RevStartTime = Time.time;
            }

            if (RevStartTime != 0 && Time.time > RevStartTime + 1.5f)
            {
                AccelAudioSource.timeSamples = UnityEngine.Random.Range(0, AccelAudioSource.clip.samples);
                AccelAudioSource.Play();
                RevStartTime = 0;
            }
        }
        else
        {
            if (AccelAudioSource.isPlaying || RevAudioSource.isPlaying)
            {
                AccelAudioSource.Stop(); RevAudioSource.Stop();
                DecelAudioSource.Play(); DecelStartTime = Time.time;
            }
            if (DecelStartTime != 0 && Time.time > DecelStartTime + 3f)
            {
                IdleAudioSource.Play();
                DecelStartTime = 0;
            }
        }

        _prevEngineTorque = WCRL.motorTorque;
    }
Пример #3
0
    void Update()
    {
        if (_gpsTimer == 0)
        {
            try { Gps.UpdateSegIdx(); }
            catch { }
            _gpsTimer = 2;
        }
        _gpsTimer--;

        #region Spray and Smoke Particles Region
        if (peDustRL.isPaused)
        {
            return;
        }
        RoadMat = Gps.RoadMat;
        bool groundedRL = WCRL.isGrounded;
        bool groundedRR = WCRR.isGrounded;

        //Spray dirt
        try
        {
            if (RoadMat == "Dirt")
            {
                //+ve ForwardSlip means spraying backwards
                //+ve SteerAngle = Right
                //SprayFL.transform.localRotation = Quaternion.Euler(45, (ForwardSlipRL >= 0 ? 0 : 180), 0);
                if (groundedRL && WCRL.angularVelocity != 0)
                {
                    peSprayFL.Play();
                    peSprayFL.startSpeed = -8;
                    // THis was for the dust - peSprayFL.startSpeed = -Mathf.Abs(ForwardSlipFL) / 2;
                }
                else
                {
                    peSprayFL.Stop();
                    peSprayFL.startSpeed = 0;
                }

                //SprayFR.transform.localRotation = Quaternion.Euler(45, (ForwardSlipRR >= 0 ? 0 : 180), 0);
                if (groundedRR && WCRR.angularVelocity != 0)
                {
                    peSprayFR.Play();
                    peSprayFR.startSpeed = -8;
                }
                else
                {
                    peSprayFR.Stop();
                    peSprayFR.startSpeed = 0;
                }
            }
            else
            {
                peSprayFR.Stop(); peSprayFL.Stop(); peSprayFR.startSpeed = 0; peSprayFL.startSpeed = 0;
            }
        }
        catch (Exception e) { Debug.Log(e.ToString()); }

        try
        {
            peDustRL.Stop();
            peDustRR.Stop();

            if (RoadMat == "DirtyRoad")
            {
                if (groundedRL)
                {
                    peDustRL.Play();
                }
                if (groundedRR)
                {
                    peDustRR.Play();
                }
                peDustRL.transform.localPosition = new Vector3(0, -0.4f, -WCRL.forwardFriction.slip / 6);
                peDustRR.transform.localPosition = new Vector3(0, -0.4f, -WCRR.forwardFriction.slip / 6);
            }
            else
            {
                peDustRL.Stop();
                peDustRR.Stop();
            }
        }
        catch { }
        #endregion
    }
Пример #4
0
    // Update is called once per frame
    //FixedUpdate is called for every physics calculation
    void FixedUpdate()
    {
        try { Gps.UpdateSegIdx(); }
        catch { }
        GetInputFromInputManager();

        //The rest of this method controls the car
        WCRL.motorTorque = v;
        WCRR.motorTorque = v;
        //FLWheel.transform.Rotate(WCFR.rpm / 10, 0, 0, Space.Self);
        //FRWheel.transform.Rotate(WCFR.rpm / 10, 0, 0, Space.Self);
        //RRWheel.transform.Rotate(WCRR.rpm / 10, 0, 0, Space.Self);
        //RLWheel.transform.Rotate(WCRL.rpm / 10, 0, 0, Space.Self);

        WCFL.steerAngle = h;
        WCFR.steerAngle = h;
        Quaternion qSteer = Quaternion.Euler(new Vector3(0, 180 + h * 2f, 0));

        FRSteering.transform.localRotation = qSteer;
        FLSteering.transform.localRotation = qSteer;

        //Adapt the tyre slip according to the road type
        string             RoadMat   = "";
        WheelFrictionCurve sFriction = WCRL.sidewaysFriction;
        WheelFrictionCurve fFriction = WCRL.forwardFriction;

        if (Gps != null)
        {
            if (Gps.IsOnRoad)
            {
                SegIdx  = Gps.CurrSegIdx;
                RoadMat = Road.Instance.Segments[SegIdx].roadMaterial;
                if (RoadMat == "Dirt")
                {
                    fFriction.stiffness = 0.1f;
                    sFriction.stiffness = 0.01f;//hit.collider.material.staticFriction;
                }
                else
                {
                    fFriction.stiffness = 0.5f;
                    sFriction.stiffness = 0.04f;
                }
            }
            else
            {
                fFriction.stiffness = 0.1f;
                sFriction.stiffness = 0.01f;
                RoadMat             = "Dirt";
            }
        }
        WCFL.forwardFriction  = fFriction;
        WCFR.forwardFriction  = fFriction;
        WCFL.sidewaysFriction = sFriction;
        WCFR.sidewaysFriction = sFriction;
        WCRL.sidewaysFriction = sFriction;
        WCRR.sidewaysFriction = sFriction;

        float travelL;
        float travelR;
        float AntiRoll = AntiRollForce * GetComponent <Rigidbody>().velocity.magnitude;

        bool  groundedFL     = WCFL.GetGroundHit(out hitFL);
        float ForwardSlipFL  = hitFL.forwardSlip;
        float SidewaysSlipFL = hitFL.sidewaysSlip;

        //float ContactAngle;
        //WheelFrictionCurve Fric;
        //Fric = WCFL.forwardFriction;
        if (groundedFL)
        {
            travelL = (-WCFL.transform.InverseTransformPoint(hitFL.point).y - WCFL.radius) / WCFL.suspensionDistance;
        }
        else
        {
            travelL = 1.0f;
        }

        //ContactAngle = Vector3.Angle (Vector3.up,WCFL.transform.InverseTransformPoint(hit.point))*2*Mathf.PI/360;
        //Fric.stiffness= Mathf.Sin (ContactAngle);
        bool  groundedFR     = WCFR.GetGroundHit(out hitFR);
        float ForwardSlipFR  = hitFR.forwardSlip;
        float SidewaysSlipFR = hitFR.sidewaysSlip;

        if (groundedFR)
        {
            travelR = (-WCFR.transform.InverseTransformPoint(hitFR.point).y - WCFR.radius) / WCFR.suspensionDistance;

            FRWheel.transform.Rotate((WCFR.rpm) / 10 - ForwardSlipFR * 40, 0, 0, Space.Self);
        }
        else
        {
            travelR = 1.0f;
            FRWheel.transform.Rotate((WCFR.rpm) / 10, 0, 0, Space.Self);
        }

        float SkidR = Mathf.Abs(SidewaysSlipFR) * (0.7f - travelR);

        if (Braking)
        {
            SkidR += 18.0f * Mathf.Abs(ForwardSlipFR);
        }
        else
        {
            SkidR += 18.0f * Mathf.Abs(ForwardSlipFR);
        }
        if (SkidR > SkidThresh && RoadMat == "Tarmac")
        {
            IsSkiddingFR = true;
        }
        else
        {
            IsSkiddingFR = false;
        }

        //ContactAngle = Vector3.Angle (Vector3.up,WCFR.transform.InverseTransformPoint(hit.point))*2*Mathf.PI/360;
        //Fric.stiffness= Mathf.Sin (ContactAngle);
        var antiRollForce = (travelL - travelR) * AntiRoll;

        if (groundedFL)
        {
            GetComponent <Rigidbody>().AddForceAtPosition(WCFL.transform.up * antiRollForce, WCFR.transform.position);
            GetComponent <Rigidbody>().AddForceAtPosition(WCFL.transform.up * -antiRollForce, WCFL.transform.position);
        }
        if (groundedFR)
        {
            GetComponent <Rigidbody>().AddForceAtPosition(WCFR.transform.up * -antiRollForce, WCFL.transform.position);
            GetComponent <Rigidbody>().AddForceAtPosition(WCFR.transform.up * antiRollForce, WCFR.transform.position);
        }

        //BACK WHEEL ANTI-ROLL BAR
        AntiRoll = 50f * GetComponent <Rigidbody>().velocity.magnitude;
        bool  groundedRL     = WCRL.GetGroundHit(out hitRL);
        float ForwardSlipRL  = hitRL.forwardSlip;
        float SidewaysSlipRL = hitRL.sidewaysSlip;

        if (groundedRL)
        {
            travelL = (-WCRL.transform.InverseTransformPoint(hitRL.point).y - WCRL.radius) / WCRL.suspensionDistance;
            RLWheel.transform.Rotate((WCRL.rpm) / 10 - ForwardSlipRL * 40, 0, 0, Space.Self);
        }
        else
        {
            travelL = 1.0f;
            RLWheel.transform.Rotate((WCRL.rpm) / 10, 0, 0, Space.Self);
        }
        float SkidL = Mathf.Abs(SidewaysSlipRL) * (0.7f - travelL);

        if (Braking)
        {
            SkidL += 18.0f * Mathf.Abs(ForwardSlipRL);
        }
        else
        {
            SkidL += 18.0f * Mathf.Abs(ForwardSlipRL);
        }
        if (SkidL > SkidThresh && RoadMat == "Tarmac")
        {
            IsSkiddingRL = true;
        }
        else
        {
            IsSkiddingRL = false;
        }

        bool  groundedRR     = WCRR.GetGroundHit(out hitRR);
        float ForwardSlipRR  = hitRR.forwardSlip;
        float SidewaysSlipRR = hitRR.sidewaysSlip;

        if (groundedRR)
        {
            travelR = (-WCRR.transform.InverseTransformPoint(hitRR.point).y - WCRR.radius) / WCRR.suspensionDistance;
        }
        else
        {
            travelR = 1.0f;
        }
        antiRollForce = (travelL - travelR) * AntiRoll;
        if (groundedRL)
        {
            GetComponent <Rigidbody>().AddForceAtPosition(WCRL.transform.up * antiRollForce, WCRR.transform.position);
            GetComponent <Rigidbody>().AddForceAtPosition(WCRL.transform.up * -antiRollForce, WCRL.transform.position);
        }
        if (groundedRR)
        {
            GetComponent <Rigidbody>().AddForceAtPosition(WCRR.transform.up * -antiRollForce, WCRL.transform.position);
            GetComponent <Rigidbody>().AddForceAtPosition(WCRR.transform.up * antiRollForce, WCRR.transform.position);
        }

        //Make Wheel ruts
        try
        {
            if (RoadMat == "Dirt" && groundedFL && !hitFL.collider.name.Contains("Car") && hitFL.force > 3200 && Road.Instance.RutCount < 150)
            {
                IsRuttingFL = true;
            }
            else
            {
                IsRuttingFL = false;
            }
            if (EndSkidmarks)
            {
                RutLeft = null;
            }
            if (IsRuttingFL)
            {
                if (!WasRuttingFL)
                {
                    //create a new rut
                    if (RutLeftNodeCount < 50)
                    {
                        GameObject.DestroyImmediate(goRutLeft); Road.Instance.RutCount--;
                    }
                    goRutLeft     = new GameObject("RutLeft");
                    goRutLeft.tag = "Rut";
                    Road.Instance.RutCount++;
                    RutLeft          = goRutLeft.AddComponent <LineRenderer>();
                    RutLeft.material = RutMatrl;
                    RutLeft.SetWidth(0.2F, 0.2F);
                    RutLeftNodeCount = 0;
                }

                RutLeftNodeCount++;
                RutLeft.SetVertexCount(RutLeftNodeCount);
                RutLeft.SetPosition(RutLeftNodeCount - 1, hitFL.point);
            }
            WasRuttingFL = IsRuttingFL;

            if (RoadMat == "Dirt" && groundedFR && !hitFR.collider.name.Contains("Car") && hitFR.force > 3000 && Road.Instance.RutCount < 150)
            {
                IsRuttingFR = true;
            }
            else
            {
                IsRuttingFR = false;
            }

            if (EndSkidmarks)
            {
                RutRight = null;
            }
            if (IsRuttingFR)
            {
                if (!WasRuttingFR)
                {
                    //create a new rut
                    if (RutRightNodeCount < 50)
                    {
                        GameObject.DestroyImmediate(goRutRight); Road.Instance.RutCount--;
                    }
                    goRutRight     = new GameObject("RutRight");
                    goRutRight.tag = "Rut";
                    Road.Instance.RutCount++;
                    RutRight          = goRutRight.AddComponent <LineRenderer>();
                    RutRight.material = RutMatrl;
                    RutRight.SetWidth(0.2F, 0.2F);
                    RutRightNodeCount = 0;
                }
                RutRightNodeCount++;
                RutRight.SetVertexCount(RutRightNodeCount);
                RutRight.SetPosition(RutRightNodeCount - 1, hitFR.point);
            }
            WasRuttingFR = IsRuttingFR;
        }
        catch (Exception e) { Debug.Log(e.ToString()); }


        //Spray dirt
        try
        {
            if (RoadMat == "Dirt")
            {
                Quaternion qSprayL = Quaternion.Euler(new Vector3(90 - Mathf.Sign(ForwardSlipFL) * 45, 180 + h * 1.5f, 0));
                SprayFL.transform.localRotation = qSprayL;
                if (groundedFL && ((ForwardSlipFL < 0 && h > 0) || (ForwardSlipFL > 0 && h < 0)) && Mathf.Abs(WCFL.rpm) > 0)
                {
                    peSprayFL.Play();
                    peSprayFL.startSpeed = -Mathf.Abs(ForwardSlipFL) * 15;
                    // THis was for the dust - peSprayFL.startSpeed = -Mathf.Abs(ForwardSlipFL) / 2;
                    peSprayFL.emissionRate = (Mathf.Abs(ForwardSlipFL) + Mathf.Abs(SidewaysSlipFL)) * 300;
                }
                else
                {
                    peSprayFL.Stop();
                }

                Quaternion qSprayR = Quaternion.Euler(new Vector3(90 - Mathf.Sign(ForwardSlipFR) * 45, 180 + h * 1.5f, 0));
                SprayFR.transform.localRotation = qSprayR;
                if (groundedFR && ((ForwardSlipFR < 0 && h < 0) || (ForwardSlipFR > 0 && h > 0)) && Mathf.Abs(WCFR.rpm) > 0)
                {
                    peSprayFR.Play();
                    peSprayFR.startSpeed   = -Mathf.Abs(ForwardSlipFR) * 15;
                    peSprayFR.emissionRate = (Mathf.Abs(ForwardSlipFR) + Mathf.Abs(SidewaysSlipFR)) * 300;
                }
                else
                {
                    peSprayFR.Stop();
                }
            }
            else
            {
                peSprayFR.Stop(); peSprayFL.Stop();
            }
        }
        catch (Exception e) { Debug.Log(e.ToString()); }

        //Skidmarks
        if (EndSkidmarks)
        {
            SkidMkLeft = null; SkidMkRight = null; WasSkiddingRL = false; WasSkiddingFR = false;
        }
        if (IsSkiddingRL)
        {
            if (!WasSkiddingRL)
            {
                //create a new rut
                if (SkidMkLeft != null)
                {
                    SkidMkLeft.enabled = false;
                }
                if (SkidMkLeftNodeCount < 50)
                {
                    GameObject.DestroyImmediate(goSkidMkLeft); Road.Instance.RutCount--;
                }
                goSkidMkLeft     = new GameObject("SkidMkLeft");
                goSkidMkLeft.tag = "Rut";
                Road.Instance.RutCount++;
                SkidMkLeft = goSkidMkLeft.AddComponent <FlatLineRenderer>();
                SkidMkLeft.Init();
                SkidMkLeft.SetMaterial(SkidMatrl);
                SkidMkLeft.Width    = 0.1f;
                SkidMkLeftNodeCount = 0;
            }
            SkidMkLeftNodeCount++;
            SkidMkLeft.AddNode(hitRL.point + Vector3.up * 0.05f);
        }
        WasSkiddingRL = IsSkiddingRL;
        if (IsSkiddingFR)
        {
            if (!WasSkiddingFR)
            {
                //create a new rut
                if (SkidMkRightNodeCount < 50)
                {
                    GameObject.DestroyImmediate(goSkidMkRight); Road.Instance.RutCount--;
                }
                goSkidMkRight     = new GameObject("SkidMkRight");
                goSkidMkRight.tag = "Rut";
                Road.Instance.RutCount++;
                SkidMkRight = goSkidMkRight.AddComponent <FlatLineRenderer>();
                SkidMkRight.Init();
                SkidMkRight.SetMaterial(SkidMatrl);
                SkidMkRight.Width    = 0.1f;
                SkidMkRightNodeCount = 0;
            }
            SkidMkRightNodeCount++;
            SkidMkRight.AddNode(hitFR.point + Vector3.up * 0.05f);
        }
        WasSkiddingFR = IsSkiddingFR;
        EndSkidmarks  = false;

        //Braking
        if (Braking)
        {
            WCFL.brakeTorque = 700;
            WCFR.brakeTorque = 700;
            WCRL.brakeTorque = 700;
            WCRR.brakeTorque = 700;
        }
        else
        {
            WCFL.brakeTorque = 0;
            WCFR.brakeTorque = 0;
            WCRL.brakeTorque = 0;
            WCRR.brakeTorque = 0;
        }
    }