private void INTERNAL_set_sidewaysFriction(ref WheelFrictionCurve value){}
		private void INTERNAL_set_forwardFriction(ref WheelFrictionCurve value){}
		private void INTERNAL_get_sidewaysFriction(out WheelFrictionCurve value){}
Beispiel #4
0
    void Awake()
    {
        carController = GetComponentInParent <RCC_CarControllerV3>();
        rigid         = carController.GetComponent <Rigidbody> ();

        // Are we going to use skidmarks? If we do, get or create SkidmarksManager on scene.
        if (!RCCSettings.dontUseSkidmarks)
        {
            if (FindObjectOfType(typeof(RCC_Skidmarks)))
            {
                skidmarks = FindObjectOfType(typeof(RCC_Skidmarks)) as RCC_Skidmarks;
            }
            else
            {
                skidmarks = (RCC_Skidmarks)Instantiate(RCCSettings.skidmarksManager, Vector3.zero, Quaternion.identity);
            }
        }

        // Increasing WheelCollider mass for avoiding unstable behavior. Only in Unity 5.
        wheelCollider.mass = rigid.mass / 15f;

        // Getting friction curves.
        forwardFrictionCurve  = wheelCollider.forwardFriction;
        sidewaysFrictionCurve = wheelCollider.sidewaysFriction;

        // Proper settings for selected behavior type.
        switch (RCCSettings.behaviorType)
        {
        case RCC_Settings.BehaviorType.SemiArcade:
            forwardFrictionCurve  = SetFrictionCurves(forwardFrictionCurve, .2f, 2f, 2f, 2f);
            sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .25f, 2f, 2f, 2f);
            wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .35f, 1f);
            break;

        case RCC_Settings.BehaviorType.Drift:
            forwardFrictionCurve  = SetFrictionCurves(forwardFrictionCurve, .25f, 1f, .8f, .5f);
            sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .4f, 1f, .5f, .75f);
            wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .1f, 1f);
            if (carController._wheelTypeChoise == RCC_CarControllerV3.WheelType.FWD)
            {
                Debug.LogError("Current behavior mode is ''Drift'', but your vehicle named " + carController.name + " was FWD. You have to use RWD, AWD, or BIASED to rear wheels. Setting it to *RWD* now. ");
                carController._wheelTypeChoise = RCC_CarControllerV3.WheelType.RWD;
            }
            break;

        case RCC_Settings.BehaviorType.Fun:
            forwardFrictionCurve  = SetFrictionCurves(forwardFrictionCurve, .2f, 2f, 2f, 2f);
            sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .25f, 2f, 2f, 2f);
            wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .75f, 2f);
            break;

        case RCC_Settings.BehaviorType.Racing:
            forwardFrictionCurve  = SetFrictionCurves(forwardFrictionCurve, .2f, 1f, .8f, .75f);
            sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .3f, 1f, .25f, .75f);
            wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .25f, 1f);
            break;

        case RCC_Settings.BehaviorType.Simulator:
            forwardFrictionCurve  = SetFrictionCurves(forwardFrictionCurve, .2f, 1f, .8f, .75f);
            sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .25f, 1f, .5f, .75f);
            wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .1f, 1f);
            break;
        }

        // Getting default stiffness.
        orgForwardStiffness  = forwardFrictionCurve.stiffness;
        orgSidewaysStiffness = sidewaysFrictionCurve.stiffness;

        // Assigning new frictons if one of the behavior preset selected above.
        wheelCollider.forwardFriction  = forwardFrictionCurve;
        wheelCollider.sidewaysFriction = sidewaysFrictionCurve;

        // Creating audiosource for skid SFX.
        if (RCCSettings.useSharedAudioSources)
        {
            if (!carController.transform.Find("All Audio Sources/Skid Sound AudioSource"))
            {
                audioSource = RCC_CreateAudioSource.NewAudioSource(carController.gameObject, "Skid Sound AudioSource", 5, 50, 0, audioClip, true, true, false);
            }
            else
            {
                audioSource = carController.transform.Find("All Audio Sources/Skid Sound AudioSource").GetComponent <AudioSource>();
            }
        }
        else
        {
            audioSource = RCC_CreateAudioSource.NewAudioSource(carController.gameObject, "Skid Sound AudioSource", 5, 50, 0, audioClip, true, true, false);
            audioSource.transform.position = transform.position;
        }

        // Creating all ground particles, and adding them to list.
        if (!RCCSettings.dontUseAnyParticleEffects)
        {
            for (int i = 0; i < RCCGroundMaterials.frictions.Length; i++)
            {
                GameObject ps = (GameObject)Instantiate(RCCGroundMaterials.frictions [i].groundParticles, transform.position, transform.rotation) as GameObject;
                emission         = ps.GetComponent <ParticleSystem> ().emission;
                emission.enabled = false;
                ps.transform.SetParent(transform, false);
                ps.transform.localPosition = Vector3.zero;
                ps.transform.localRotation = Quaternion.identity;
                allWheelParticles.Add(ps.GetComponent <ParticleSystem> ());
            }
        }
    }
		private void INTERNAL_get_forwardFriction(out WheelFrictionCurve value){}
Beispiel #6
0
    // Use this for initialization


    void Start()
    {
        deadTexture           = Resources.Load <Material>("Materials/gray");
        defaultMaterial       = Resources.Load <Material>("Materials/defaultMaterial");
        defaultPhMat          = Resources.Load <PhysicMaterial>("PhysicsMaterials/defaultObject");
        transparentRedTexture = Resources.Load <Material>("Materials/redTransparent");
        intenceRed            = Resources.Load <Material>("Materials/intenceRed");
        // SET BASE CAR
        Rigidbody carRB = gameObject.AddComponent <Rigidbody>();

        carRB.mass                   = 50;
        carRB.drag                   = 0;
        carRB.angularDrag            = 0.05f;
        carRB.collisionDetectionMode = CollisionDetectionMode.Continuous;
        carRB.centerOfMass           = Vector3.zero;

        GameObject[] carColliders = FindGameObjectInChildWithName(transform, "collider");

        GameObject carMesh = FindGameObjectInChildWithName(transform, "mesh")[0];

        //carMesh.GetComponent<MeshRenderer>().material = defaultMaterial;
        foreach (GameObject collider in carColliders)
        {
            MeshCollider meshColl = carMesh.AddComponent <MeshCollider>();
            meshColl.sharedMesh = collider.GetComponent <MeshFilter>().sharedMesh;
            meshColl.convex     = true;
            meshColl.material   = defaultPhMat;
            collider.SetActive(false);
        }

        // LOAD WHEELS
        wheelsDirAxis = FindGameObjectInChildWithName(transform, "direction");
        wheelsRotAxis = FindGameObjectInChildWithName(transform, "rotation");

        wheelsCollider = new WheelCollider[wheelsRotAxis.Length];

        JointSpring spring = new JointSpring();

        spring.spring         = 8000;
        spring.damper         = 100;
        spring.targetPosition = 0.3f;

        WheelFrictionCurve forwardFrCurve = new WheelFrictionCurve();

        forwardFrCurve.extremumSlip   = 1.4f;
        forwardFrCurve.extremumValue  = 1f;
        forwardFrCurve.asymptoteSlip  = 1.8f;
        forwardFrCurve.asymptoteValue = 0.5f;
        forwardFrCurve.stiffness      = 1.3f;

        WheelFrictionCurve sideFrCurve = new WheelFrictionCurve();

        sideFrCurve.extremumSlip   = 1.2f;
        sideFrCurve.extremumValue  = 1f;
        sideFrCurve.asymptoteSlip  = 1.5f;
        sideFrCurve.asymptoteValue = 0.75f;
        sideFrCurve.stiffness      = 1f;

        GameObject wheelAsset = Resources.Load <GameObject>("defaultWheel");

        Mesh wheelMesh     = wheelAsset.transform.Find("wheelMesh").GetComponent <MeshFilter>().sharedMesh;
        Mesh wheelAxisMesh = wheelAsset.transform.Find("wheelAxisMesh").GetComponent <MeshFilter>().sharedMesh;
        Mesh wheelRad      = wheelAsset.transform.Find("wheelRadius").GetComponent <MeshFilter>().sharedMesh;
        Mesh wheelColl     = wheelAsset.transform.Find("wheelCollider").GetComponent <MeshFilter>().sharedMesh;

        float wheelRadius = wheelRad.bounds.size.x;

        Quaternion rotationR = Quaternion.LookRotation(transform.forward, transform.right);


        for (int a = 0; a < wheelsRotAxis.Length; a++)
        {
            GameObject wheel = new GameObject("wheelMesh");
            GameObject axis  = new GameObject("axisMesh");

            wheel.layer = gameObject.layer;
            wheel.tag   = gameObject.tag;
            axis.layer  = gameObject.layer;
            axis.tag    = gameObject.tag;

            initMesh(wheel, wheelMesh, defaultMaterial);
            initMesh(axis, wheelAxisMesh, defaultMaterial);

            wheel.transform.position = wheelsRotAxis[a].transform.position;
            wheel.transform.parent   = wheelsRotAxis[a].transform;
            MeshCollider meshColl = wheel.AddComponent <MeshCollider>();
            meshColl.sharedMesh     = wheelColl;
            meshColl.convex         = true;
            meshColl.material       = defaultPhMat;
            axis.transform.position = wheelsRotAxis[a].transform.parent.position;
            axis.transform.parent   = wheelsRotAxis[a].transform.parent;

            wheel.transform.rotation     = rotationR;
            axis.transform.localRotation = wheel.transform.localRotation;

            GameObject collider = new GameObject("wheelCollider");

            collider.layer = gameObject.layer;
            collider.tag   = gameObject.tag;

            collider.transform.parent   = wheelsRotAxis[a].transform.parent;
            collider.transform.position = wheelsRotAxis[a].transform.position;

            wheelsCollider[a]                    = collider.AddComponent <WheelCollider>();
            wheelsCollider[a].mass               = 10;
            wheelsCollider[a].radius             = wheelRadius;
            wheelsCollider[a].wheelDampingRate   = 0.2f;
            wheelsCollider[a].suspensionDistance = 0.01f;
            wheelsCollider[a].suspensionSpring   = spring;
            wheelsCollider[a].forwardFriction    = forwardFrCurve;
            wheelsCollider[a].sidewaysFriction   = sideFrCurve;
        }

        GameObject[] tractionObjects = FindGameObjectInChildWithName(transform, "traction");
        wheelsTractionCollider = new WheelCollider[tractionObjects.Length];

        for (int a = 0; a < tractionObjects.Length; a++)
        {
            wheelsTractionCollider[a] = tractionObjects[a].GetComponentInChildren <WheelCollider>();
        }
    }
Beispiel #7
0
    // Update is called once per frame
    void Update()
    {
        int accel = 0;

        if (Input.GetKey(KeyCode.W))
        {
            accel++;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            accel--;
        }

        if (accel == 0)
        {
            accelResetTimer += Time.deltaTime;
            accelerator      = Mathf.Lerp(accelerator, 0f, accelResetTimer);
        }
        else
        {
            accelResetTimer = 0f;
            accelerator    += accel * Time.deltaTime * 5f;
        }

        accelerator = Mathf.Clamp(accelerator, -1f, 1f);

        Debug.Log(accelerator);

        int direction = 0;

        if (Input.GetKey(KeyCode.A))
        {
            direction--;
        }

        if (Input.GetKey(KeyCode.D))
        {
            direction++;
        }

        // Reset wheel angle to center if no input is read
        if (direction == 0)
        {
            turnResetTimer += 2f * Time.deltaTime;
            turnAngle       = Mathf.Lerp(turnAngle, 0f, turnResetTimer);
        }
        else
        {
            turnResetTimer = 0f;
            turnAngle     += direction * TurnSpeed * Time.deltaTime;
        }

        turnAngle = Mathf.Clamp(turnAngle, -MAX_ANGLE, MAX_ANGLE);

        // Set turning angle for wheels
        WCFLeft.steerAngle = WCFRight.steerAngle = turnAngle;

        // Set torque for wheels
        WCFLeft.motorTorque     = WCFRight.motorTorque =
            WCRLeft.motorTorque = WCRRight.motorTorque =
                accelerator * MaxTorque;


        if (Input.GetKey(KeyCode.S))
        {
            WCRLeft.brakeTorque = WCRRight.brakeTorque = WCFLeft.brakeTorque = WCFRight.brakeTorque = 2350f;
        }
        else
        {
            WCRLeft.brakeTorque = WCRRight.brakeTorque = WCFLeft.brakeTorque = WCFRight.brakeTorque = 0f;
        }

        WheelFrictionCurve curve = WCRRight.sidewaysFriction;

        if (Input.GetKey(KeyCode.Space))
        {
            WCRLeft.brakeTorque = WCRRight.brakeTorque = 2350f;
            WCRLeft.motorTorque = WCRRight.motorTorque = 0f;

            //WCFLeft.motorTorque = WCFRight.motorTorque = accelerator * MaxTorque * 2f;

            curve.extremumValue      = 0.8f;
            WCRLeft.sidewaysFriction = WCRRight.sidewaysFriction = curve;
        }
        else
        {
            curve.extremumValue      = 1.0f;
            WCRLeft.sidewaysFriction = WCRRight.sidewaysFriction = curve;
        }

        // Update wheel positions
        UpdateWheel(WCFLeft, WFLeft);
        UpdateWheel(WCFRight, WFRight);
        UpdateWheel(WCRLeft, WRLeft);
        UpdateWheel(WCRRight, WRRight);

        WRRight.Rotate(0f, 180f, 0f);
        WFRight.Rotate(0f, 180f, 0f);
    }
    void FixedUpdate()
    {
        float delta = Time.fixedDeltaTime;

        GetComponent <Rigidbody>().centerOfMass = shiftCentre;

        float steer = 0;     // steering -1.0 .. 1.0
        float accel = 0.0f;  // accelerating -1.0 .. 1.0
        bool  brake = false; // braking (true is brake)


        if ((checkForActive == null) || checkForActive.active)
        {
            // we only look at input when the object we monitor is
            // active (or we aren't monitoring an object).
            if (controll)
            {
                steer = Input.GetAxis("Horizontal");
                accel = Input.GetAxis("Vertical");
                brake = Input.GetButton("Jump");
                //steer=GetComponent<sc_vehicle>().steer;
                if (Mathf.Abs(steer) >= 0.9f && speed > 80)
                {
                    brake = true;
                }
            }
        }

        if (carSteer)
        {
            carSteer.localEulerAngles = new Vector3(0, 0, -steer * 100);
        }

        // handle automatic shifting
        if (automatic && (currentGear == 1) && (accel < 0.0f))
        {
            if (speed < 5.0f)
            {
                ShiftDown(); // reverse
            }
        }
        else if (automatic && (currentGear == 0) && (accel > 0.0f))
        {
            if (speed < 5.0f)
            {
                ShiftUp(); // go from reverse to first gear
            }
        }
        else if (automatic && (motorRPM > shiftUpRPM) && (accel > 0.0f) && !brake)
        {
            // if (speed > 20)
            ShiftUp(); // shift up
        }
        else if (automatic && (motorRPM < shiftDownRPM) && (currentGear > 1))
        {
            ShiftDown(); // shift down
        }



        if ((currentGear == 0))
        {
            shiftCentre.z = -accel / 3;
            if (speed < gears[0] * -10)
            {
                accel = -accel; // in automatic mode we need to hold arrow down for reverse
            }
        }
        else
        {
            shiftCentre.z = -(accel / currentGear) / 2;
        }


        shiftCentre.x = -Mathf.Clamp(steer * (speed / 100), -0.1f, 0.1f);



        // the RPM we try to achieve.
        wantedRPM = (5500.0f * accel) * 0.1f + wantedRPM * 0.9f;

        float rpm             = 0.0f;
        int   motorizedWheels = 0;
        bool  floorContact    = false;
        int   currentWheel    = 0;

        // calc rpm from current wheel speed and do some updating


        // rigidbody.AddForce(-Vector3.up * 5000);

        foreach (WheelData w in wheels)
        {
            WheelHit      hit;
            WheelCollider col = w.col;

            // only calculate rpm on wheels that are connected to engine
            if (w.motor)
            {
                if (brake && currentGear < 2)
                {
                    rpm += accel * idleRPM;
                    if (rpm > 1)
                    {
                        shiftCentre.z = Mathf.PingPong(Time.time * (accel * 10), 1.0f) - 0.5f;
                    }
                    else
                    {
                        shiftCentre.z = 0.0f;
                    }
                }
                else
                {
                    rpm += col.rpm;
                }


                motorizedWheels++;
            }



            if (brake || accel < 0.0f)
            {
                if ((accel < 0.0f) || (brake && (w == wheels[2] || w == wheels[3])))
                {
                    if (brake && (accel > 0.0f))
                    {
                        brackF = Mathf.Lerp(brackF, 10, accel * Time.deltaTime);
                    }
                    else
                    {
                        brackF = Mathf.Lerp(brackF, 5, 0.2f * Time.deltaTime);
                    }

                    wantedRPM       = 0.0f;
                    col.brakeTorque = brakeTorque;
                    w.rotation      = curRotation;
                }
            }
            else
            {
                col.brakeTorque = accel == 0 ? col.brakeTorque = 200 : col.brakeTorque = 1;

                brackF = speed > 1 ?

                         (speed > 20 ? brackF = Mathf.Lerp(brackF, 1, 0.01f) : brackF = Mathf.Lerp(brackF, 3.0f, 0.01f))

                    : brackF = Mathf.Lerp(brackF, 0.0f, 0.01f);

                curRotation = w.rotation;
            }



            // like i would but just try out a fiddle with it.
            WheelFrictionCurve fc = col.forwardFriction;


            fc.asymptoteValue   = 5000.0f;
            fc.extremumSlip     = 2.0f;
            fc.asymptoteSlip    = 20.0f;
            fc.stiffness        = (stiffness / brackF);
            col.forwardFriction = fc;
            fc                   = col.sidewaysFriction;
            fc.stiffness         = stiffness / brackF;
            col.sidewaysFriction = fc;


            // calculate the local rotation of the wheels from the delta time and rpm
            // then set the local rotation accordingly (also adjust for steering)

            w.rotation = Mathf.Repeat(w.rotation + delta * col.rpm * 360.0f / 60.0f, 360.0f);
            w.transform.localRotation = Quaternion.Euler(w.rotation, col.steerAngle, 0.0f);



            // let the wheels contact the ground, if no groundhit extend max suspension distance
            Vector3 lp = w.transform.localPosition;

            if (col.GetGroundHit(out hit))
            {
                lp.y        -= Vector3.Dot(w.transform.position - hit.point, Vector3.up / transform.lossyScale.x) - (col.radius);
                lp.y         = Mathf.Clamp(lp.y, -10.0f, w.curY);
                floorContact = floorContact || (w.motor);
            }
            else
            {
                lp.y = w.startPos.y - suspensionDistance;
            }

            currentWheel++;
            w.transform.localPosition = lp;
        }
        // calculate the actual motor rpm from the wheels connected to the engine
        // note we haven't corrected for gear yet.
        if (motorizedWheels > 1)
        {
            rpm = rpm / motorizedWheels;
        }

        // we do some delay of the change (should take delta instead of just 95% of
        // previous rpm, and also adjust or gears.
        motorRPM = 0.95f * motorRPM + 0.05f * Mathf.Abs(rpm * gears[currentGear]);
        if (motorRPM > 5500.0f)
        {
            motorRPM = 5200.0f;
        }

        // calculate the 'efficiency' (low or high rpm have lower efficiency then the
        // ideal efficiency, say 2000RPM, see table
        int index = (int)(motorRPM / efficiencyTableStep);

        if (index >= efficiencyTable.Length)
        {
            index = efficiencyTable.Length - 1;
        }
        if (index < 0)
        {
            index = 0;
        }

        // calculate torque using gears and efficiency table
        float newTorque = torque * gears[currentGear] * efficiencyTable[index];

        // go set torque to the wheels
        foreach (WheelData w in wheels)
        {
            WheelCollider col = w.col;

            // of course, only the wheels connected to the engine can get engine torque
            if (w.motor)
            {
                // only set torque if wheel goes slower than the expected speed
                if (Mathf.Abs(col.rpm) > Mathf.Abs(wantedRPM))
                {
                    // wheel goes too fast, set torque to 0
                    col.motorTorque = 0;
                }
                else
                {
                    //
                    float curTorque = col.motorTorque;

                    if (!brake)
                    {
                        col.motorTorque = curTorque * 0.9f + newTorque * 0.1f;
                    }
                }
            }
            // check if we have to brake


            // set steering angle
            if (brake)
            {
                col.steerAngle = Mathf.Lerp(col.steerAngle, steer * w.maxSteer, 0.02f);
            }
            else
            {
                float SteerAngle = Mathf.Clamp((speed / transform.lossyScale.x) / maxSteerAngle, 1.0f, maxSteerAngle);
                col.steerAngle = steer * (w.maxSteer / SteerAngle);
            }
        }



        // if we have an audiosource (motorsound) adjust pitch using rpm
        if (audioSource != null)
        {
            // calculate pitch (keep it within reasonable bounds)
            float pitch = Mathf.Clamp(1.0f + ((motorRPM - idleRPM) / (shiftUpRPM - idleRPM) * 0.5f), 1.0f, 10.0f);
            audioSource.pitch  = pitch;
            audioSource.volume = Mathf.MoveTowards(audioSource.volume, 1.0f, 0.02f);
        }
    }
    // Use this for initialization
    void Start()
    {
        controller      = transform.FindChild("controller");
        audio_skid.loop = true;
        wheels          = new WheelData[4];


        // setup wheels
        bool frontDrive = (wheelDrive == JWheelDrive.Front) || (wheelDrive == JWheelDrive.All);
        bool backDrive  = (wheelDrive == JWheelDrive.Back) || (wheelDrive == JWheelDrive.All);

        // we use 4 wheels, but you can change that easily if neccesary.
        // this is the only place that refers directly to wheelFL, ...
        // so when adding wheels, you need to add the public transforms,
        // adjust the array size, and add the wheels initialisation here.
        wheels[0] = SetWheelParams(wheelFR, maxSteerAngle, frontDrive, wheelFR.localPosition.y);
        wheels[1] = SetWheelParams(wheelFL, maxSteerAngle, frontDrive, wheelFL.localPosition.y);
        wheels[2] = SetWheelParams(wheelBR, 0.0f, backDrive, wheelBR.localPosition.y);
        wheels[3] = SetWheelParams(wheelBL, 0.0f, backDrive, wheelBL.localPosition.y);


        // found out the hard way: some parameters must be set AFTER all wheel colliders
        // are created, like wheel mass, otherwise your car will act funny and will
        // flip over all the time.
        foreach (WheelData w in wheels)
        {
            WheelCollider col = w.col;
            col.suspensionDistance = suspensionDistance;
            JointSpring js = col.suspensionSpring;
            js.spring            = springs;
            js.damper            = dampers;
            col.suspensionSpring = js;



            col.mass = wheelWeight;

            // see docs, haven't really managed to get this work
            // like i would but just try out a fiddle with it.
            WheelFrictionCurve fc = col.forwardFriction;
            fc.asymptoteValue   = 5000.0f;
            fc.extremumSlip     = 2.0f;
            fc.asymptoteSlip    = 20.0f;
            fc.stiffness        = stiffness;
            col.forwardFriction = fc;
            fc = col.sidewaysFriction;
            fc.asymptoteValue    = 7500.0f;
            fc.asymptoteSlip     = 2.0f;
            fc.stiffness         = stiffness;
            col.sidewaysFriction = fc;
        }

        // we move the centre of mass (somewhere below the centre works best.)


        // shortcut to audioSource should be engine sound, if null then no engine sound.
        audioSource = (AudioSource)GetComponent(typeof(AudioSource));
        if (audioSource == null)
        {
            Debug.Log("No audio source, add one to the car with looping engine noise (but can be turned off");
        }
    }
Beispiel #10
0
    void FixedUpdate()
    {
        speed = myRigidbody.velocity.magnitude * 2.7f;

        if (crash)
        {
            myRigidbody.constraints  = RigidbodyConstraints.None;
            myRigidbody.centerOfMass = Vector3.zero;
        }
        else
        {
            myRigidbody.constraints  = RigidbodyConstraints.FreezeRotationZ;
            myRigidbody.centerOfMass = bikeSetting.shiftCentre;
        }
        if (activeControl)
        {
            accel = 0.0f;
            shift = false;
            brake = false;

            if (!crash)
            {
                steer = Mathf.MoveTowards(steer, Input.GetAxis("Horizontal"), 0.1f);
                accel = Input.GetAxis("Vertical");
                brake = Input.GetButton("Jump");
                shift = Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift);
            }
            else
            {
                steer = 0;
            }
        }
        else
        {
            accel = 0.0f;
            steer = 0.0f;
            shift = false;
            brake = false;
        }

        if (bikeSetting.automaticGear && (currentGear == 1) && (accel < 0.0f))
        {
            if (speed < 1.0f)
            {
                ShiftDown();
            }
        }
        else if (bikeSetting.automaticGear && (currentGear == 0) && (accel > 0.0f))
        {
            if (speed < 5.0f)
            {
                ShiftUp();
            }
        }
        else if (bikeSetting.automaticGear && (motorRPM > bikeSetting.shiftUpRPM) && (accel > 0.0f) && speed > 10.0f && !brake)
        {
            ShiftUp();
        }
        else if (bikeSetting.automaticGear && (motorRPM < bikeSetting.shiftDownRPM) && (currentGear > 1))
        {
            ShiftDown();
        }

        if (speed < 1.0f)
        {
            Backward = true;
        }

        if (currentGear == 0 && Backward == true)
        {
            if (speed < bikeSetting.gears[0] * -10)
            {
                accel = -accel;
            }
        }
        else
        {
            Backward = false;
        }

        //속도 5500->550
        wantedRPM = (550.0f * accel) * 0.1f + wantedRPM * 0.9f;

        float rpm             = 0.0f;
        int   motorizedWheels = 0;
        bool  floorContact    = false;
        int   currentWheel    = 0;

        foreach (WheelComponent w in wheels)
        {
            WheelHit      hit;
            WheelCollider col = w.collider;

            if (w.drive)
            {
                if (!NeutralGear && brake && currentGear < 2)
                {
                    rpm += accel * bikeSetting.idleRPM;

                    if (rpm > 1)
                    {
                        bikeSetting.shiftCentre.z = Mathf.PingPong(Time.time * (accel * 10), 0.5f) - 0.25f;
                    }
                    else
                    {
                        bikeSetting.shiftCentre.z = 0.0f;
                    }
                }
                else
                {
                    if (!NeutralGear)
                    {
                        rpm += col.rpm;
                    }
                    else
                    {
                        rpm += ((bikeSetting.idleRPM * 2.0f) * accel);
                    }
                }
                motorizedWheels++;
            }

            if (crash)
            {
                w.collider.enabled = false;
                w.wheel.GetComponent <Collider>().enabled = true;
            }
            else
            {
                w.collider.enabled = true;
                w.wheel.GetComponent <Collider>().enabled = false;
            }

            if (brake || accel < 0.0f)
            {
                if ((accel < 0.0f) || (brake && w == wheels[1]))
                {
                    if (brake && (accel > 0.0f))
                    {
                        slip = Mathf.Lerp(slip, bikeSetting.slipBrake, accel * 0.01f);
                    }
                    else if (speed > 1.0f)
                    {
                        slip = Mathf.Lerp(slip, 1.0f, 0.002f);
                    }
                    else
                    {
                        slip = Mathf.Lerp(slip, 1.0f, 0.02f);
                    }
                    wantedRPM       = 0.0f;
                    col.brakeTorque = bikeSetting.brakePower;
                    w.rotation      = w_rotate;
                }
            }
            else
            {
                col.brakeTorque = accel == 0 ? col.brakeTorque = 3000 : col.brakeTorque = 0;
                slip            = Mathf.Lerp(slip, 1.0f, 0.02f);
                w_rotate        = w.rotation;
            }
            WheelFrictionCurve fc = col.forwardFriction;

            if (w == wheels[1])
            {
                fc.stiffness        = bikeSetting.stiffness / slip;
                col.forwardFriction = fc;
                fc                   = col.sidewaysFriction;
                fc.stiffness         = bikeSetting.stiffness / slip;
                col.sidewaysFriction = fc;
            }

            if (shift && (currentGear > 1 && speed > 50.0f) && shifmotor)
            {
                shifting = true;
                if (powerShift == 0)
                {
                    shifmotor = false;
                }

                powerShift = Mathf.MoveTowards(powerShift, 0.0f, Time.deltaTime * 10.0f);

                curTorque = powerShift > 0 ? bikeSetting.shiftPower : bikeSetting.bikePower;
            }
            else
            {
                shifting = false;

                if (powerShift > 20)
                {
                    shifmotor = true;
                }
                powerShift = Mathf.MoveTowards(powerShift, 100.0f, Time.deltaTime * 5.0f);
                curTorque  = bikeSetting.bikePower;
            }

            w.rotation            = Mathf.Repeat(w.rotation + Time.deltaTime * col.rpm * 360.0f / 60.0f, 360.0f);
            w.wheel.localRotation = Quaternion.Euler(w.rotation, 0.0f, 0.0f);

            Vector3 lp = w.axle.localPosition;

            if (col.GetGroundHit(out hit) && (w == wheels[1] || (w == wheels[0] && Wheelie == 0)))
            {
                lp.y -= Vector3.Dot(w.wheel.position - hit.point, transform.TransformDirection(0, 1, 0)) - (col.radius);
                lp.y  = Mathf.Clamp(lp.y, w.startPos.y - bikeWheels.setting.Distance,
                                    w.startPos.y + bikeWheels.setting.Distance);

                floorContact = floorContact || (w.drive);

                if (!crash)
                {
                    myRigidbody.angularDrag = 10.0f;
                }
                else
                {
                    myRigidbody.angularDrag = 0.0f;
                }

                grounded = true;

                if (w.collider.GetComponent <WheelSkidmarks>())
                {
                    w.collider.GetComponent <WheelSkidmarks>().enabled = true;
                }
            }
            else
            {
                grounded = false;

                if (w.collider.GetComponent <WheelSkidmarks>())
                {
                    w.collider.GetComponent <WheelSkidmarks>().enabled = false;
                }

                lp.y = w.startPos.y - bikeWheels.setting.Distance;

                if (!wheels[0].collider.isGrounded && !wheels[1].collider.isGrounded)
                {
                    myRigidbody.centerOfMass = new Vector3(0, 0.2f, 0);
                    myRigidbody.angularDrag  = 1.0f;

                    myRigidbody.AddForce(0, -10000, 0);
                }
            }
            currentWheel++;
            w.axle.localPosition = lp;
        }

        if (motorizedWheels > 1)
        {
            rpm = rpm / motorizedWheels;
        }

        motorRPM = 0.95f * motorRPM + 0.05f * Mathf.Abs(rpm * bikeSetting.gears[currentGear]);
        if (motorRPM > 5500.0f)
        {
            motorRPM = 5200.0f;
        }

        int index = (int)(motorRPM / efficiencyTableStep);

        if (index >= efficiencyTable.Length)
        {
            index = efficiencyTable.Length - 1;
        }
        if (index < 0)
        {
            index = 0;
        }

        float newTorque = curTorque * bikeSetting.gears[currentGear] * efficiencyTable[index];

        foreach (WheelComponent w in wheels)
        {
            WheelCollider col = w.collider;

            if (w.drive)
            {
                if (Mathf.Abs(col.rpm) > Mathf.Abs(wantedRPM))
                {
                    col.motorTorque = 0;
                }
                else
                {
                    float curTorqueCol = col.motorTorque;

                    if (!brake && accel != 0 && NeutralGear == false)
                    {
                        if ((speed < bikeSetting.LimitForwardSpeed && currentGear > 0) ||
                            (speed < bikeSetting.LimitBackwardSpeed && currentGear == 0))
                        {
                            col.motorTorque = curTorqueCol * 0.9f + newTorque * 1.0f;
                        }
                        else
                        {
                            col.motorTorque = 0;
                            col.brakeTorque = 2000;
                        }
                    }
                    else
                    {
                        col.motorTorque = 0;
                    }
                }
            }
            float SteerAngle = Mathf.Clamp((speed) / bikeSetting.maxSteerAngle, 1.0f, bikeSetting.maxSteerAngle);
            col.steerAngle = steer * (w.maxSteer / SteerAngle);
        }
        shiftTime = Mathf.MoveTowards(shiftTime, 0.0f, 0.1f);
    }
Beispiel #11
0
    public override void OnInspectorGUI()
    {
        RG_CarController carController = (RG_CarController)target;

        //     Texture racingAssetTexture = Resources.Load("RacingAssetTexture") as Texture;
        //
        //     GUIStyle inspectorStyle = new GUIStyle(GUI.skin.label);
        //     inspectorStyle.fixedWidth = 256;
        //     inspectorStyle.fixedHeight = 64;
        //     inspectorStyle.margin = new RectOffset((int)(((Screen.width * 0.98f) - 264) / 2), 0, 0, 0);
        //GUILayout.Label(racingAssetTexture, inspectorStyle);

        EditorGUILayout.BeginVertical("Box");
        showControllerSettings = EditorGUILayout.Foldout(showControllerSettings, "Controller Settings");
        if (showControllerSettings)
        {
            ///Car Max Steer Angle
            SerializedProperty maxSteerAngle = serializedObject.FindProperty("m_MaximumSteerAngle");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(maxSteerAngle, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Max Steer Angle Top Speed
            SerializedProperty maxSteerAngleTopSpeed = serializedObject.FindProperty("m_SteerAngleAtMaxSpeed");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(maxSteerAngleTopSpeed, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Steer Helper
            SerializedProperty steerHelper = serializedObject.FindProperty("m_SteerHelper");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(steerHelper, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            EditorGUILayout.BeginVertical("Box");
            ///Car Traction Control
            SerializedProperty tractionControl = serializedObject.FindProperty("m_TractionControl");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(tractionControl, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Level " + carController.levelTireTraction.ToString(), GUILayout.MaxWidth(Screen.width * 0.14f));
            SerializedProperty levelBonusTireTraction = serializedObject.FindProperty("levelBonusTireTraction");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(levelBonusTireTraction, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginVertical("Box");

            ///Car Steer Sensetivity
            SerializedProperty steerSensitivity = serializedObject.FindProperty("m_SteerSensitivity");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(steerSensitivity, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Steer Sensetivity
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Level " + carController.levelSteerSensitivity.ToString(), GUILayout.MaxWidth(Screen.width * 0.14f));
            SerializedProperty levelBonusSteerSensitivity = serializedObject.FindProperty("levelBonusSteerSensitivity");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(levelBonusSteerSensitivity, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            ///
            ///         TOP SPEED
            ///
            EditorGUILayout.BeginVertical("Box");

            ///Car Top Speed
            SerializedProperty topSpeed = serializedObject.FindProperty("m_Topspeed");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(topSpeed, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Level Bonus Top Speed
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Level " + carController.levelTopSpeed.ToString(), GUILayout.MaxWidth(Screen.width * 0.14f));
            SerializedProperty levelBonusTopSpeed = serializedObject.FindProperty("levelBonusTopSpeed");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(levelBonusTopSpeed, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            ///
            ///         TORQUE / ACCELERATION
            ///
            EditorGUILayout.BeginVertical("Box");

            ///Car Torque
            SerializedProperty torque = serializedObject.FindProperty("m_FullTorqueOverAllWheels");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(torque, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Level Bonus Acceleration - Torque is used as a variable for increasing acceleration rate.
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Level " + carController.levelAcceleration.ToString(), GUILayout.MaxWidth(Screen.width * 0.14f));
            SerializedProperty levelBonusAcceleration = serializedObject.FindProperty("levelBonusAcceleration");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(levelBonusAcceleration, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();
            ///
            ///         BRAKE TORQUE / BRAKE POWER
            ///
            EditorGUILayout.BeginVertical("Box");

            SerializedProperty brakeTorque = serializedObject.FindProperty("m_BrakeTorque");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(brakeTorque, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Level " + carController.levelBrakePower.ToString(), GUILayout.MaxWidth(Screen.width * 0.14f));
            SerializedProperty levelBonusBrakePower = serializedObject.FindProperty("levelBonusBrakePower");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(levelBonusBrakePower, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            ///
            ///         Car Reverse Torque
            ///
            SerializedProperty reverseTorque = serializedObject.FindProperty("m_ReverseTorque");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(reverseTorque, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }
            ///
            ///         Car Downforce
            ///
            SerializedProperty downforce = serializedObject.FindProperty("m_Downforce");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(downforce, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Rev Range Boundary
            SerializedProperty revRangeBoundary = serializedObject.FindProperty("m_RevRangeBoundary");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(revRangeBoundary, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Slip Limit
            SerializedProperty slipLimit = serializedObject.FindProperty("m_SlipLimit");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(slipLimit, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Centre of Mass Offset
            SerializedProperty comOffset = serializedObject.FindProperty("m_CentreOfMassOffset");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(comOffset, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Drive Type
            SerializedProperty carDriveType = serializedObject.FindProperty("m_CarDriveType");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(carDriveType, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Speed Type
            SerializedProperty speedType = serializedObject.FindProperty("m_SpeedType");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(speedType, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }
        }
        showNitroSettings = EditorGUILayout.Foldout(showNitroSettings, "Nitro Settings");
        if (showNitroSettings)
        {
            ///Car Nitro Top Speed Limit Increase
            SerializedProperty nitroTopSpeed = serializedObject.FindProperty("nitroTopSpeed");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(nitroTopSpeed, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Nitro Full Torque Limit Increase
            SerializedProperty nitroFullTorque = serializedObject.FindProperty("nitroFullTorque");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(nitroFullTorque, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Nitro Duration
            SerializedProperty nitroDuration = serializedObject.FindProperty("nitroDuration");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(nitroDuration, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Nitro Spend Rate
            SerializedProperty nitroSpendRate = serializedObject.FindProperty("nitroSpendRate");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(nitroSpendRate, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Nitro Refill Rate
            SerializedProperty nitroRefillRate = serializedObject.FindProperty("nitroRefillRate");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(nitroRefillRate, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Nitro ON Event Trigger
            SerializedProperty nitroON = serializedObject.FindProperty("nitroON");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(nitroON, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Nitro OFF Event Trigger
            SerializedProperty nitroOFF = serializedObject.FindProperty("nitroOFF");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(nitroOFF, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            ///Car Nitro OFF Event Trigger
            SerializedProperty nitroFX = serializedObject.FindProperty("nitroFX");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(nitroFX, true);
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }
        }

        showWheelSettings = EditorGUILayout.Foldout(showWheelSettings, "Wheel Settings");
        if (showWheelSettings)
        {
            EditorGUILayout.BeginVertical("Box");
            if (GUILayout.Button("Update Additional Wheel Settings"))
            {
                TurnTheGameOn.RG_NetworkWheels obj = ((MonoBehaviour)target).gameObject.GetComponent <TurnTheGameOn.RG_NetworkWheels> ();
                obj.UpdateWheelSettings();
            }

            SerializedProperty wheelColliders = serializedObject.FindProperty("m_WheelColliders");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(wheelColliders, true, GUILayout.MaxWidth(Screen.width * 0.9f));
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            SerializedProperty wheelMeshes = serializedObject.FindProperty("m_WheelMeshes");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(wheelMeshes, true, GUILayout.MaxWidth(Screen.width * 0.9f));
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            SerializedProperty wheelEffects = serializedObject.FindProperty("m_WheelEffects");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(wheelEffects, true, GUILayout.MaxWidth(Screen.width * 0.9f));
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("", GUILayout.MaxWidth(Screen.width * 0.29f));
            EditorGUILayout.LabelField("FR", GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.LabelField("FL", GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.LabelField("RR", GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.LabelField("RL", GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginVertical("Box");
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Mass", GUILayout.MaxWidth(Screen.width * 0.25f));
            carController.m_WheelColliders[0].mass = EditorGUILayout.FloatField(carController.m_WheelColliders[0].mass, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[1].mass = EditorGUILayout.FloatField(carController.m_WheelColliders[1].mass, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[2].mass = EditorGUILayout.FloatField(carController.m_WheelColliders[2].mass, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[3].mass = EditorGUILayout.FloatField(carController.m_WheelColliders[3].mass, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Radius", GUILayout.MaxWidth(Screen.width * 0.25f));
            carController.m_WheelColliders[0].radius = EditorGUILayout.FloatField(carController.m_WheelColliders[0].radius, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[1].radius = EditorGUILayout.FloatField(carController.m_WheelColliders[1].radius, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[2].radius = EditorGUILayout.FloatField(carController.m_WheelColliders[2].radius, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[3].radius = EditorGUILayout.FloatField(carController.m_WheelColliders[3].radius, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Wheel Damping Rate", GUILayout.MaxWidth(Screen.width * 0.25f));
            carController.m_WheelColliders[0].wheelDampingRate = EditorGUILayout.FloatField(carController.m_WheelColliders[0].wheelDampingRate, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[1].wheelDampingRate = EditorGUILayout.FloatField(carController.m_WheelColliders[1].wheelDampingRate, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[2].wheelDampingRate = EditorGUILayout.FloatField(carController.m_WheelColliders[2].wheelDampingRate, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[3].wheelDampingRate = EditorGUILayout.FloatField(carController.m_WheelColliders[3].wheelDampingRate, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Suspension Distance", GUILayout.MaxWidth(Screen.width * 0.25f));
            carController.m_WheelColliders[0].suspensionDistance = EditorGUILayout.FloatField(carController.m_WheelColliders[0].suspensionDistance, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[1].suspensionDistance = EditorGUILayout.FloatField(carController.m_WheelColliders[1].suspensionDistance, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[2].suspensionDistance = EditorGUILayout.FloatField(carController.m_WheelColliders[2].suspensionDistance, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[3].suspensionDistance = EditorGUILayout.FloatField(carController.m_WheelColliders[3].suspensionDistance, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Force App Point Distance", GUILayout.MaxWidth(Screen.width * 0.25f));
            carController.m_WheelColliders[0].forceAppPointDistance = EditorGUILayout.FloatField(carController.m_WheelColliders[0].forceAppPointDistance, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[1].forceAppPointDistance = EditorGUILayout.FloatField(carController.m_WheelColliders[1].forceAppPointDistance, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[2].forceAppPointDistance = EditorGUILayout.FloatField(carController.m_WheelColliders[2].forceAppPointDistance, GUILayout.MaxWidth(Screen.width * 0.14f));
            carController.m_WheelColliders[3].forceAppPointDistance = EditorGUILayout.FloatField(carController.m_WheelColliders[3].forceAppPointDistance, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();



            EditorGUILayout.BeginVertical("Box");
            EditorGUILayout.LabelField("Suspension Spring");
            frjs = carController.m_WheelColliders[0].suspensionSpring;
            fljs = carController.m_WheelColliders[1].suspensionSpring;
            rrjs = carController.m_WheelColliders[2].suspensionSpring;
            rljs = carController.m_WheelColliders[3].suspensionSpring;
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Spring", GUILayout.MaxWidth(Screen.width * 0.25f));
            frjs.spring = EditorGUILayout.FloatField(frjs.spring, GUILayout.MaxWidth(Screen.width * 0.14f));
            fljs.spring = EditorGUILayout.FloatField(fljs.spring, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrjs.spring = EditorGUILayout.FloatField(rrjs.spring, GUILayout.MaxWidth(Screen.width * 0.14f));
            rljs.spring = EditorGUILayout.FloatField(rljs.spring, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Damper", GUILayout.MaxWidth(Screen.width * 0.25f));
            frjs.damper = EditorGUILayout.FloatField(frjs.damper, GUILayout.MaxWidth(Screen.width * 0.14f));
            fljs.damper = EditorGUILayout.FloatField(fljs.damper, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrjs.damper = EditorGUILayout.FloatField(rrjs.damper, GUILayout.MaxWidth(Screen.width * 0.14f));
            rljs.damper = EditorGUILayout.FloatField(rljs.damper, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Target Position", GUILayout.MaxWidth(Screen.width * 0.25f));
            frjs.targetPosition = EditorGUILayout.FloatField(frjs.targetPosition, GUILayout.MaxWidth(Screen.width * 0.14f));
            fljs.targetPosition = EditorGUILayout.FloatField(fljs.targetPosition, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrjs.targetPosition = EditorGUILayout.FloatField(rrjs.targetPosition, GUILayout.MaxWidth(Screen.width * 0.14f));
            rljs.targetPosition = EditorGUILayout.FloatField(rljs.targetPosition, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            carController.m_WheelColliders[0].suspensionSpring = frjs;
            carController.m_WheelColliders[1].suspensionSpring = fljs;
            carController.m_WheelColliders[2].suspensionSpring = rrjs;
            carController.m_WheelColliders[3].suspensionSpring = rljs;
            EditorGUILayout.EndVertical();



            EditorGUILayout.BeginVertical("Box");
            EditorGUILayout.LabelField("Forward Friction");
            frwfc = carController.m_WheelColliders[0].forwardFriction;
            flwfc = carController.m_WheelColliders[1].forwardFriction;
            rrwfc = carController.m_WheelColliders[2].forwardFriction;
            rlwfc = carController.m_WheelColliders[3].forwardFriction;
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Extremum Slip", GUILayout.MaxWidth(Screen.width * 0.25f));
            frwfc.extremumSlip = EditorGUILayout.FloatField(frwfc.extremumSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            flwfc.extremumSlip = EditorGUILayout.FloatField(flwfc.extremumSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrwfc.extremumSlip = EditorGUILayout.FloatField(rrwfc.extremumSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlwfc.extremumSlip = EditorGUILayout.FloatField(rlwfc.extremumSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Extremum Value", GUILayout.MaxWidth(Screen.width * 0.25f));
            frwfc.extremumValue = EditorGUILayout.FloatField(frwfc.extremumValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            flwfc.extremumValue = EditorGUILayout.FloatField(flwfc.extremumValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrwfc.extremumValue = EditorGUILayout.FloatField(rrwfc.extremumValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlwfc.extremumValue = EditorGUILayout.FloatField(rlwfc.extremumValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Asmptote Slip", GUILayout.MaxWidth(Screen.width * 0.25f));
            frwfc.asymptoteSlip = EditorGUILayout.FloatField(frwfc.asymptoteSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            flwfc.asymptoteSlip = EditorGUILayout.FloatField(flwfc.asymptoteSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrwfc.asymptoteSlip = EditorGUILayout.FloatField(rrwfc.asymptoteSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlwfc.asymptoteSlip = EditorGUILayout.FloatField(rlwfc.asymptoteSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Asymptote Value", GUILayout.MaxWidth(Screen.width * 0.25f));
            frwfc.asymptoteValue = EditorGUILayout.FloatField(frwfc.asymptoteValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            flwfc.asymptoteValue = EditorGUILayout.FloatField(flwfc.asymptoteValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrwfc.asymptoteValue = EditorGUILayout.FloatField(rrwfc.asymptoteValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlwfc.asymptoteValue = EditorGUILayout.FloatField(rlwfc.asymptoteValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Stiffness", GUILayout.MaxWidth(Screen.width * 0.25f));
            frwfc.stiffness = EditorGUILayout.FloatField(frwfc.stiffness, GUILayout.MaxWidth(Screen.width * 0.14f));
            flwfc.stiffness = EditorGUILayout.FloatField(flwfc.stiffness, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrwfc.stiffness = EditorGUILayout.FloatField(rrwfc.stiffness, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlwfc.stiffness = EditorGUILayout.FloatField(rlwfc.stiffness, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            carController.m_WheelColliders[0].forwardFriction = frwfc;
            carController.m_WheelColliders[1].forwardFriction = flwfc;
            carController.m_WheelColliders[2].forwardFriction = rrwfc;
            carController.m_WheelColliders[3].forwardFriction = rlwfc;
            EditorGUILayout.EndVertical();



            EditorGUILayout.BeginVertical("Box");
            EditorGUILayout.LabelField("Sidways Friction");
            frswfc = carController.m_WheelColliders[0].sidewaysFriction;
            flswfc = carController.m_WheelColliders[1].sidewaysFriction;
            rrswfc = carController.m_WheelColliders[2].sidewaysFriction;
            rlswfc = carController.m_WheelColliders[3].sidewaysFriction;
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Extremum Slip", GUILayout.MaxWidth(Screen.width * 0.25f));
            frswfc.extremumSlip = EditorGUILayout.FloatField(frswfc.extremumSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            flswfc.extremumSlip = EditorGUILayout.FloatField(flswfc.extremumSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrswfc.extremumSlip = EditorGUILayout.FloatField(rrswfc.extremumSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlswfc.extremumSlip = EditorGUILayout.FloatField(rlswfc.extremumSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Extremum Value", GUILayout.MaxWidth(Screen.width * 0.25f));
            frswfc.extremumValue = EditorGUILayout.FloatField(frswfc.extremumValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            flswfc.extremumValue = EditorGUILayout.FloatField(flswfc.extremumValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrswfc.extremumValue = EditorGUILayout.FloatField(rrswfc.extremumValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlswfc.extremumValue = EditorGUILayout.FloatField(rlswfc.extremumValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Asmptote Slip", GUILayout.MaxWidth(Screen.width * 0.25f));
            frswfc.asymptoteSlip = EditorGUILayout.FloatField(frswfc.asymptoteSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            flswfc.asymptoteSlip = EditorGUILayout.FloatField(flswfc.asymptoteSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrswfc.asymptoteSlip = EditorGUILayout.FloatField(rrswfc.asymptoteSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlswfc.asymptoteSlip = EditorGUILayout.FloatField(rlswfc.asymptoteSlip, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Asymptote Value", GUILayout.MaxWidth(Screen.width * 0.25f));
            frswfc.asymptoteValue = EditorGUILayout.FloatField(frswfc.asymptoteValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            flswfc.asymptoteValue = EditorGUILayout.FloatField(flswfc.asymptoteValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrswfc.asymptoteValue = EditorGUILayout.FloatField(rrswfc.asymptoteValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlswfc.asymptoteValue = EditorGUILayout.FloatField(rlswfc.asymptoteValue, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Stiffness", GUILayout.MaxWidth(Screen.width * 0.25f));
            frswfc.stiffness = EditorGUILayout.FloatField(frswfc.stiffness, GUILayout.MaxWidth(Screen.width * 0.14f));
            flswfc.stiffness = EditorGUILayout.FloatField(flswfc.stiffness, GUILayout.MaxWidth(Screen.width * 0.14f));
            rrswfc.stiffness = EditorGUILayout.FloatField(rrswfc.stiffness, GUILayout.MaxWidth(Screen.width * 0.14f));
            rlswfc.stiffness = EditorGUILayout.FloatField(rlswfc.stiffness, GUILayout.MaxWidth(Screen.width * 0.14f));
            EditorGUILayout.EndHorizontal();
            carController.m_WheelColliders[0].sidewaysFriction = frswfc;
            carController.m_WheelColliders[1].sidewaysFriction = flswfc;
            carController.m_WheelColliders[2].sidewaysFriction = rrswfc;
            carController.m_WheelColliders[3].sidewaysFriction = rlswfc;
            EditorGUILayout.EndVertical();

            EditorGUILayout.EndVertical();
        }


        EditorGUILayout.EndVertical();
    }
Beispiel #12
0
    void AISteer()
    {
        Vector3 localTarget = transform.InverseTransformPoint(AiAgent.position);

        angleToTarget = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;

        AISteerValue = angleToTarget * Mathf.Sign(currSpeed);
        steerAngle   = Mathf.Clamp(AISteerValue, -maxSteerAngle, maxSteerAngle);

        if (wt == WheelType.Wheels)
        {
            if (Mathf.Abs(angleToTarget) < maxSteerAngle)
            {
                if (!reversing)
                {
                    Wheels[0].steerAngle = steerAngle;
                    Wheels[1].steerAngle = steerAngle;
                }
                else
                {
                    brake = true;
                    Invoke(nameof(BackToDrive), 1);
                }
            }
            else
            {
                reversing            = true;
                Wheels[0].steerAngle = -steerAngle;
                Wheels[1].steerAngle = -steerAngle;
            }
        }
        if (wt == WheelType.Tracks)
        {
            if (Mathf.Abs(angleToTarget) < maxSteerAngle)
            {
                for (int i = 0; i < Wheels.Length; i++)
                {
                    WheelFrictionCurve swf = Wheels[i].sidewaysFriction;
                    swf.stiffness = 7;
                    Wheels[i].sidewaysFriction = swf;
                }

                if (!spinning)
                {
                    horizontalInput = 0;
                    if (angleToTarget > 5 && angleToTarget < maxSteerAngle)
                    {
                        leftTrackSteeringFactor  = 0.5f * steerSensitivity;
                        rightTrackSteeringFactor = -0.5f * steerSensitivity;
                    }
                    if (angleToTarget < -5 && angleToTarget > -maxSteerAngle)
                    {
                        leftTrackSteeringFactor  = -0.5f * steerSensitivity;
                        rightTrackSteeringFactor = 0.5f * steerSensitivity;
                    }
                }
                else
                {
                    brake    = true;
                    spinning = false;

                    Invoke(nameof(BackToDrive), 2);
                }
            }
            else
            {
                if (currSpeed > 0.1)
                {
                    if (!spinning)
                    {
                        brake = true;
                    }
                }
                else
                {
                    spinning        = true;
                    horizontalInput = 1;

                    for (int i = 0; i < Wheels.Length; i++)
                    {
                        WheelFrictionCurve swf = Wheels[i].sidewaysFriction;
                        swf.stiffness = originalStiffness;
                        Wheels[i].sidewaysFriction = swf;
                    }

                    if (angleToTarget > maxSteerAngle && angleToTarget < 179)
                    {
                        leftTrackSteeringFactor  = -1.3f * steerSensitivity;
                        rightTrackSteeringFactor = 1.3f * steerSensitivity;
                    }
                    if (angleToTarget < -maxSteerAngle && angleToTarget > -179)
                    {
                        leftTrackSteeringFactor  = 1.3f * steerSensitivity;
                        rightTrackSteeringFactor = -1.3f * steerSensitivity;
                    }
                }
            }
        }
    }