示例#1
0
    //move the wheels based on their suspension
    void UpdateWheelPositions()
    {
        WheelHit contact = new WheelHit();

        if (wheelFL.GetGroundHit(out contact))
        {
            Vector3 temp = wheelFL.transform.position;
            temp.y = (contact.point + (wheelFL.transform.up * wheelFL.radius)).y;
            wheelTransformFL.position = temp;
        }
        if (wheelFR.GetGroundHit(out contact))
        {
            Vector3 temp = wheelFR.transform.position;
            temp.y = (contact.point + (wheelFR.transform.up * wheelFR.radius)).y;
            wheelTransformFR.position = temp;
        }
        if (wheelBL.GetGroundHit(out contact))
        {
            Vector3 temp = wheelBL.transform.position;
            temp.y = (contact.point + (wheelBL.transform.up * wheelBL.radius)).y;
            wheelTransformBL.position = temp;
        }
        if (wheelBR.GetGroundHit(out contact))
        {
            Vector3 temp = wheelBR.transform.position;
            temp.y = (contact.point + (wheelBR.transform.up * wheelBR.radius)).y;
            wheelTransformBR.position = temp;
        }
    }
示例#2
0
    private void SteerHelper()
    {
        foreach (var axleInfo in axleInfos)
        {
            WheelHit[] wheelHit = new WheelHit[2];
            axleInfo.leftWheel.GetGroundHit(out wheelHit[0]);
            axleInfo.rightWheel.GetGroundHit(out wheelHit[1]);
            foreach (var wh in wheelHit)
            {
                if (wh.normal == Vector3.zero)
                {
                    return; // wheels arent on the ground so dont realign the rigidbody velocity
                }
            }
        }

// this if is needed to avoid gimbal lock problems that will make the car suddenly shift direction
        if (Mathf.Abs(CurrentRotation - transform.eulerAngles.y) < 10f)
        {
            var        turnAdjust  = (transform.eulerAngles.y - CurrentRotation);
            Quaternion velRotation = Quaternion.AngleAxis(turnAdjust, Vector3.up);
            m_Rigidbody.velocity = velRotation * m_Rigidbody.velocity;
        }

        CurrentRotation = transform.eulerAngles.y;
    }
示例#3
0
    //move wheels based on their suspension.
    void UpdateWheelPositions()
    {
        WheelHit contact = new WheelHit();

        if (wheelFL.GetGroundHit(out contact))
        {
            Vector3 temp = wheelFL.transform.position;
            wheelTransformFL.position = temp;
        }
        if (wheelFR.GetGroundHit(out contact))
        {
            Vector3 temp = wheelFR.transform.position;
            wheelTransformFR.position = temp;
        }
        if (wheelBL.GetGroundHit(out contact))
        {
            Vector3 temp = wheelBL.transform.position;
            wheelTransformBL.position = temp;
        }
        if (wheelBR.GetGroundHit(out contact))
        {
            Vector3 temp = wheelBR.transform.position;
            wheelTransformBR.position = temp;
        }
    }
示例#4
0
    void FixedUpdate()
    {
        WheelHit hit       = new WheelHit();
        float    travelL   = 1f;
        float    travelR   = 1f;
        bool     groundedL = WheelL.GetGroundHit(out hit);

        if (groundedL)
        {
            travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
        }

        bool groundedR = WheelR.GetGroundHit(out hit);

        if (groundedR)
        {
            travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;
        }

        float antiRollForce = (travelL - travelR) * AntiRoll;

        if (groundedL)
        {
            GetComponent <Rigidbody>().AddForceAtPosition(WheelL.transform.up * -antiRollForce, WheelL.transform.position);
        }
        if (groundedR)
        {
            GetComponent <Rigidbody>().AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position);
        }
    }
示例#5
0
    void UpdateWheelHeight(Transform wheelTransform, WheelCollider collider)
    {
        Vector3 localPosition = wheelTransform.localPosition;

        WheelHit hit = new WheelHit();

        // see if we have contact with ground

        if (collider.GetGroundHit(out hit))
        {
            float hitY = collider.transform.InverseTransformPoint(hit.point).y;

            localPosition.y = hitY + collider.radius;
        }
        else
        {
            // no contact with ground, just extend wheel position with suspension distance

            localPosition = Vector3.Lerp(localPosition, -Vector3.up * collider.suspensionDistance, .05f);
        }

        // actually update the position

        //wheelTransform.localPosition = localPosition;
        wheelTransform.localPosition = wheelTransform.localPosition;

        wheelTransform.localRotation = Quaternion.Euler(0, collider.steerAngle, 0);
    }
示例#6
0
    private void Start()
    {
        whellHit = new WheelHit();

        rb = GetComponent <Rigidbody>();
        selectWheelHit();
    }
示例#7
0
    void UpdateWheelPositions()
    {
        WheelHit Contact = new WheelHit();

        if (WheelFront_Left.GetGroundHit(out Contact))
        {
            Vector3 temp = WheelFront_Left.transform.position;
            temp.y = (Contact.point + (WheelFront_Left.transform.up * WheelFront_Left.radius)).y;
            WheelTransformFL.position = temp;
        }
        if (WheelFront_Right.GetGroundHit(out Contact))
        {
            Vector3 temp = WheelFront_Right.transform.position;
            temp.y = (Contact.point + (WheelFront_Right.transform.up * WheelFront_Right.radius)).y;
            WheelTransformFR.position = temp;
        }
        if (WheelBack_Right.GetGroundHit(out Contact))
        {
            Vector3 temp = WheelBack_Right.transform.position;
            temp.y = (Contact.point + (WheelBack_Right.transform.up * WheelBack_Right.radius)).y;
            WheelTransformBR.position = temp;
        }
        if (WheelBack_Left.GetGroundHit(out Contact))
        {
            Vector3 temp = WheelBack_Left.transform.position;
            temp.y = (Contact.point + (WheelBack_Left.transform.up * WheelBack_Left.radius)).y;
            WheelTransformBL.position = temp;
        }
    }
示例#8
0
    private void FixedUpdate()
    {
        WheelHit hit       = default(WheelHit);
        float    num       = 1f;
        float    num2      = 1f;
        float    num3      = 1f;
        float    num4      = 1f;
        bool     groundHit = m_wheelFL.GetGroundHit(out hit);

        if (groundHit)
        {
            Vector3 vector = m_wheelFL.transform.InverseTransformPoint(hit.point);
            num = (0f - vector.y - m_wheelFL.radius) / m_wheelFL.suspensionDistance;
        }
        bool groundHit2 = m_wheelFR.GetGroundHit(out hit);

        if (groundHit2)
        {
            Vector3 vector2 = m_wheelFR.transform.InverseTransformPoint(hit.point);
            num2 = (0f - vector2.y - m_wheelFR.radius) / m_wheelFR.suspensionDistance;
        }
        bool groundHit3 = m_wheelRL.GetGroundHit(out hit);

        if (groundHit3)
        {
            Vector3 vector3 = m_wheelRL.transform.InverseTransformPoint(hit.point);
            num3 = (0f - vector3.y - m_wheelRL.radius) / m_wheelRL.suspensionDistance;
        }
        bool groundHit4 = m_wheelRR.GetGroundHit(out hit);

        if (groundHit4)
        {
            Vector3 vector4 = m_wheelRR.transform.InverseTransformPoint(hit.point);
            num4 = (0f - vector4.y - m_wheelRR.radius) / m_wheelRR.suspensionDistance;
        }
        float       num5              = num - num2;
        JointSpring suspensionSpring  = m_wheelFL.suspensionSpring;
        float       num6              = num5 * suspensionSpring.spring * m_antiRoll;
        float       num7              = num3 - num4;
        JointSpring suspensionSpring2 = m_wheelRL.suspensionSpring;
        float       num8              = num7 * suspensionSpring2.spring * m_antiRoll;

        if (groundHit)
        {
            base.rigidbody.AddForceAtPosition(m_wheelFL.transform.up * (0f - num6), m_wheelFL.transform.position);
        }
        if (groundHit2)
        {
            base.rigidbody.AddForceAtPosition(m_wheelFR.transform.up * num6, m_wheelFR.transform.position);
        }
        if (groundHit3)
        {
            base.rigidbody.AddForceAtPosition(m_wheelRL.transform.up * (0f - num8), m_wheelRL.transform.position);
        }
        if (groundHit4)
        {
            base.rigidbody.AddForceAtPosition(m_wheelRR.transform.up * num8, m_wheelRR.transform.position);
        }
    }
示例#9
0
 public float Dampness(WheelHit wheelhit)
 {//returns ground dampness value. if in a puddle, return 1.
     if (wheelhit.collider.gameObject.CompareTag("puddle"))
     {
         return(1f);
     }
     return(groundDampness * 0.001f);
 }
示例#10
0
    static int _CreateWheelHit(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 0);
        WheelHit obj = new WheelHit();

        LuaScriptMgr.PushValue(L, obj);
        return(1);
    }
    public void CheckCollisions(WheelHit coll)
    {
        if (coll.collider.transform.GetComponent <TerrainType>())
        {
            TerrainType terr = coll.collider.transform.GetComponent <TerrainType>();

            if (makeBumpSounds)
            {
                //if (coll.force > (requiredForceForSmallBump + additionalForceRequiredForBigBump)) {
                //    PlayBigBump();
                //    Debug.Log("BUMP");
                //}
                //else {
                //    if (coll.force > requiredForceForSmallBump) {
                //        PlaySmallBump();
                //        Debug.Log("bump");
                //    }
                //}
                float verticalDisplacement = this.transform.position.y - previousVerticalPosition;
                float newVerticalVelocity  = verticalDisplacement / Time.deltaTime;

                if (newVerticalVelocity - previousVerticalVelocity > requiredVelocityChangeForSmallBump + additionalVelocityChangeRequiredForBigBump)
                {
                    PlayBigBump();
                    Debug.Log("BUMP");
                }
                else
                {
                    if (newVerticalVelocity - previousVerticalVelocity > requiredVelocityChangeForSmallBump)
                    {
                        PlaySmallBump();
                        Debug.Log("bump");
                    }
                }

                previousVerticalPosition = this.transform.position.y;
                previousVerticalVelocity = newVerticalVelocity;
            }

            if (currentlyPlayingContinuousSound)
            {
                if (terr.SurfaceType != currentGroundSurface)
                {
                    PlayContinuousSound(terr.SurfaceType);
                }
            }
            else
            {
                if (terr.SurfaceType == currentGroundSurface)
                {
                    PlayContinuousSound(terr.SurfaceType);
                    currentlyPlayingContinuousSound = true;
                }
            }

            currentGroundSurface = terr.SurfaceType;
        }
    }
示例#12
0
    // Update is called once per frame
    void Update()
    {
        WheelHit wheelHit = new WheelHit();

        if (collider.GetGroundHit(out wheelHit))
        {
            transform.position = wheelHit.point + new Vector3(0.0f, collider.radius, 0.0f);
        }
    }
示例#13
0
    void SetMotorTorque(WheelHit hitFL, WheelHit hitFR)
    {
        if (!thrustEnabled)
        {
            return;
        }

        var velocityIsForward = Vector3.Angle(transform.forward, _rigidbody.velocity) < 50f;

        // Determine if the cursor key input means braking
        var doBraking = _currentSpeedKmh > 0.5f &&
                        (Input.GetAxis("Vertical") < 0 && velocityIsForward ||
                         Input.GetAxis("Vertical") > 0 && !velocityIsForward);
        bool doFullBrake = Input.GetKey("space");

        _doSkidmarking = _carIsNotOnSand && (doFullBrake || hitFL.sidewaysSlip <-0.45f || hitFR.sidewaysSlip> 0.45f) && _currentSpeedKmh > 20.0f;


        SetBrakeSound(_doSkidmarking);
        SetSkidmarking(_doSkidmarking);

        if (doBraking || doFullBrake)
        {
            float brakeTorque = doFullBrake ? fullBrakeTorque : maxTorque;

            wheelColliderFL.brakeTorque = brakeTorque;
            wheelColliderFR.brakeTorque = brakeTorque;
            wheelColliderRL.brakeTorque = brakeTorque;
            wheelColliderRR.brakeTorque = brakeTorque;
            wheelColliderFL.motorTorque = 0;
            wheelColliderFR.motorTorque = 0;
        }
        else
        {
            wheelColliderFL.brakeTorque = 0;
            wheelColliderFR.brakeTorque = 0;
            wheelColliderRL.brakeTorque = 0;
            wheelColliderRR.brakeTorque = 0;

            var torque = maxTorque * Input.GetAxis("Vertical");

            //var direction = this._isMovingForward ? "Forward" : "Backwards";
            //Debug.Log($"Moving {direction} with speed: {_currentSpeedKmh} km/h");

            if ((velocityIsForward && _currentSpeedKmh >= this._maxSpeedKmh) ||
                (!velocityIsForward && _currentSpeedKmh >= this._maxSpeedBackwardKmh))
            {
                //No further acceleration if top speeds are reached
                torque = 0;
            }

            wheelColliderFL.motorTorque = torque;
            wheelColliderFR.motorTorque = torque;
        }
    }
示例#14
0
    // ImplementaciГіn de la barra estabilizadora

    void FixedUpdate()
    {
        WheelHit hitL = new WheelHit();
        WheelHit hitR = new WheelHit();

        bool groundedL = WheelL.GetGroundHit(out hitL);

        if (groundedL)
        {
            m_extensionL = (-WheelL.transform.InverseTransformPoint(hitL.point).y - WheelL.radius) / WheelL.suspensionDistance;
        }
        else
        {
            m_extensionL = 1.0f;
        }

        bool groundedR = WheelR.GetGroundHit(out hitR);

        if (groundedR)
        {
            m_extensionR = (-WheelR.transform.InverseTransformPoint(hitR.point).y - WheelR.radius) / WheelR.suspensionDistance;
        }
        else
        {
            m_extensionR = 1.0f;
        }

        m_antiRollRatio = Bias(m_extensionL - m_extensionR, AntiRollBias);
        m_antiRollForce = m_antiRollRatio * AntiRoll * AntiRollFactor;

        // Modo Strict: Afecta al caso en que una rueda estГЎ levantada y la otra apoyada.
        // Si se quita una fuerza en un sitio, reponerla en otro para mantener el peso total constante.
        //
        // - Strict 0: desactivado, sГіlo hay fuerza en la rueda apoyada.
        // - Strict 1: reponer la fuerza de la rueda levantada en el centro de masas.
        // - Strict 2: aplicar las fuerzas en las ruedas independientemente de que estГ©n apoyadas o no.

        if (groundedL || StrictMode == 2)
        {
            GetComponent <Rigidbody>().AddForceAtPosition(WheelL.transform.up * -m_antiRollForce, WheelL.transform.position);
        }
        else if (StrictMode == 1)
        {
            GetComponent <Rigidbody>().AddForce(WheelL.transform.up * -m_antiRollForce);
        }

        if (groundedR || StrictMode == 2)
        {
            GetComponent <Rigidbody>().AddForceAtPosition(WheelR.transform.up * m_antiRollForce, WheelR.transform.position);
        }
        else if (StrictMode == 1)
        {
            GetComponent <Rigidbody>().AddForce(WheelL.transform.up * m_antiRollForce);
        }
    }
示例#15
0
 void ActiveDifferential(WheelHit wheelL, WheelHit wheelR)
 {
     if (wheelL.forwardSlip + wheelR.forwardSlip > 0.5f)
     {
         carBehaviour.FrontWheelDriveBias = 0.5f;
     }
     else
     {
         carBehaviour.FrontWheelDriveBias = Mathf.Abs(wheelL.forwardSlip + wheelR.forwardSlip);
     }
 }
示例#16
0
    static int GetGroundHit(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        WheelCollider obj  = LuaScriptMgr.GetNetObject <WheelCollider>(L, 1);
        WheelHit      arg0 = LuaScriptMgr.GetNetObject <WheelHit>(L, 2);
        bool          o    = obj.GetGroundHit(out arg0);

        LuaScriptMgr.Push(L, o);
        LuaScriptMgr.PushValue(L, arg0);
        return(2);
    }
示例#17
0
        private void WheelHitSurface(WheelCollider wheel, WheelHit hit)
        {
            WheelFrictionCurve fFriction = wheel.forwardFriction;

            fFriction.stiffness   = hit.collider.material.staticFriction;
            wheel.forwardFriction = fFriction;
            WheelFrictionCurve sFriction = wheel.sidewaysFriction;

            sFriction.stiffness    = hit.collider.material.staticFriction;
            wheel.sidewaysFriction = sFriction;
        }
示例#18
0
    private float FFBApproximation(WheelCollider wheelFL, WheelHit wheelHitL, WheelCollider wheelFR, WheelHit wheelHitR)
    {
        // left side
        if (Mathf.Abs(wheelHitL.sidewaysSlip) < wheelFL.sidewaysFriction.extremumSlip)
        {
            // from 0 to extremum
            forceL     = Mathf.Lerp(0.0f, wheelFL.sidewaysFriction.extremumValue, Mathf.Abs(wheelHitL.sidewaysSlip) / wheelFL.sidewaysFriction.extremumSlip);
            forceLCase = 1;
        }
        else if (Mathf.Abs(wheelHitL.sidewaysSlip) < wheelFL.sidewaysFriction.asymptoteSlip)
        {
            // from extremum to asymptote
            forceL     = Mathf.Lerp(wheelFL.sidewaysFriction.extremumValue, wheelFL.sidewaysFriction.asymptoteSlip, Mathf.Abs(wheelHitL.sidewaysSlip / wheelFL.sidewaysFriction.extremumSlip));
            forceLCase = 2;
        }
        else
        {
            // post asymptote
            forceL     = wheelFL.sidewaysFriction.asymptoteValue;
            forceLCase = 3;
        }
        // right side
        if (Mathf.Abs(wheelHitR.sidewaysSlip) < wheelFR.sidewaysFriction.extremumSlip)
        {
            // from 0 to extremum
            forceR     = Mathf.Lerp(0.0f, wheelFR.sidewaysFriction.extremumValue, Mathf.Abs(wheelHitR.sidewaysSlip / wheelFR.sidewaysFriction.extremumSlip));
            forceRCase = 1;
        }
        else if (Mathf.Abs(wheelHitR.sidewaysSlip) < wheelFR.sidewaysFriction.asymptoteSlip)
        {
            // from extremum to asymptote
            forceR     = Mathf.Lerp(wheelFR.sidewaysFriction.extremumValue, wheelFR.sidewaysFriction.asymptoteSlip, Mathf.Abs(wheelHitR.sidewaysSlip / wheelFR.sidewaysFriction.extremumSlip));
            forceRCase = 2;
        }
        else
        {
            // post asymptote
            forceR     = wheelFR.sidewaysFriction.asymptoteValue;
            forceRCase = 3;
        }

        // base force

        /*if (forceL > 0 && forceL < 0.1) forceL = 0.1f;
         * if (forceL < 0 && forceL > -0.1) forceL = -0.1f;
         * if (forceR > 0 && forceR < 0.1) forceR = 0.1f;
         * if (forceR < 0 && forceR > -0.1) forceR = -0.1f;*/

        forceLdebug = forceL;
        forceRdebug = forceR;
        return(forceL + forceR);
    }
示例#19
0
 // Update is called once per frame
 void Update()
 {
     wheelsOnGrass = 0;
     for (int i = 0; i < WheelColliders.Length; i++)
     {
         WheelHit hit = new WheelHit();
         if (WheelColliders[i].GetGroundHit(out hit) && hit.collider.name == "RoadGrass")
         {
             wheelsOnGrass++;
         }
     }
     GetComponent <UnityStandardAssets.Vehicles.Car.CarController>().m_SteerHelper = 1 - (wheelsOnGrass * 0.08f);
 }
示例#20
0
    private void FixedUpdate()
    {
        WheelHit wheelHit  = default(WheelHit);
        float    num       = 1f;
        float    num2      = 1f;
        float    num3      = 1f;
        float    num4      = 1f;
        bool     groundHit = this.m_wheelFL.GetGroundHit(out wheelHit);

        if (groundHit)
        {
            num = (-this.m_wheelFL.transform.InverseTransformPoint(wheelHit.point).y - this.m_wheelFL.radius) / this.m_wheelFL.suspensionDistance;
        }
        bool groundHit2 = this.m_wheelFR.GetGroundHit(out wheelHit);

        if (groundHit2)
        {
            num2 = (-this.m_wheelFR.transform.InverseTransformPoint(wheelHit.point).y - this.m_wheelFR.radius) / this.m_wheelFR.suspensionDistance;
        }
        bool groundHit3 = this.m_wheelRL.GetGroundHit(out wheelHit);

        if (groundHit3)
        {
            num3 = (-this.m_wheelRL.transform.InverseTransformPoint(wheelHit.point).y - this.m_wheelRL.radius) / this.m_wheelRL.suspensionDistance;
        }
        bool groundHit4 = this.m_wheelRR.GetGroundHit(out wheelHit);

        if (groundHit4)
        {
            num4 = (-this.m_wheelRR.transform.InverseTransformPoint(wheelHit.point).y - this.m_wheelRR.radius) / this.m_wheelRR.suspensionDistance;
        }
        float num5 = (num - num2) * this.m_wheelFL.suspensionSpring.spring * this.m_antiRoll;
        float num6 = (num3 - num4) * this.m_wheelRL.suspensionSpring.spring * this.m_antiRoll;

        if (groundHit)
        {
            base.rigidbody.AddForceAtPosition(this.m_wheelFL.transform.up * -num5, this.m_wheelFL.transform.position);
        }
        if (groundHit2)
        {
            base.rigidbody.AddForceAtPosition(this.m_wheelFR.transform.up * num5, this.m_wheelFR.transform.position);
        }
        if (groundHit3)
        {
            base.rigidbody.AddForceAtPosition(this.m_wheelRL.transform.up * -num6, this.m_wheelRL.transform.position);
        }
        if (groundHit4)
        {
            base.rigidbody.AddForceAtPosition(this.m_wheelRR.transform.up * num6, this.m_wheelRR.transform.position);
        }
    }
示例#21
0
    void FixedUpdate()
    {
        _currentSpeedKMH  = Vector3.Project(_rigidBody.velocity, _transform.forward).magnitude * 3.6f; //To prevent falling in some random direction from being accounted towards the current speed
        _isHeadingForward = Vector3.Angle(_rigidBody.velocity, transform.forward) < 90;

        WheelHit hitFL = GetGroundInfos(ref wheelColliderFL, ref _groundTagFL, ref _groundTextureFL);
        WheelHit hitRL = GetGroundInfos(ref wheelColliderRL, ref _groundTagRL, ref _groundTextureRL);

        _carIsOnDrySand = _groundTagFL.CompareTo("Terrain") == 0 && _groundTextureFL == 0;
        _carIsOnStone   = _groundTagFL.CompareTo("Terrain") == 0 && _groundTextureFL == 2;
        _carSlips       = hitRL.sidewaysSlip > 0.3f || hitRL.sidewaysSlip < -0.3f;

        _doSkidmarking = false;

        if (isFullBraking())
        {
            _doSkidmarking = _carIsOnStone && _currentSpeedKMH > 20.0f;

            SetBreakTorque(fullBrakeTorque);
            SetMotorTorque(0);
        }
        else if (thrustEnabled)
        {
            if (isBraking())
            {
                SetBreakTorque(maxBrakeTorque);
                SetMotorTorque(0);
            }
            else
            {
                float accelerationModifyer = _isHeadingForward ? (_currentSpeedKMH > MaxSpeedKmh ? 0 : 1) : (_currentSpeedKMH > MaxReverseSpeedKmh ? 0 : 1);
                SetBreakTorque(0);
                SetMotorTorque(maxTorque * Input.GetAxis("Vertical") * accelerationModifyer);
            }
        }

        SetBrakeSound(_doSkidmarking);
        SetSkidmarking(_doSkidmarking || _carSlips);

        SetSteerAngle(maxSteerAngle * Input.GetAxis("Horizontal"));

        int   gearNum   = 0;
        float engineRPM = kmh2rpm(_currentSpeedKMH, out gearNum);

        SetEngineSound(engineRPM);

        SetParticleSystems(engineRPM);
    }
    void UpdateWheelHeight(Transform wheelTransform, WheelCollider collider)
    {
        Vector3 localPosition = wheelTransform.localPosition;

        WheelHit hit = new WheelHit();

        // see if we have contact with ground

        if (collider.GetGroundHit(out hit))
        {
            float hitY = collider.transform.InverseTransformPoint(hit.point).y;

            localPosition.y = hitY + collider.radius;

            //wheelCollider.GetComponent<ParticleSystem>().enableEmission = true;
            if (
                Mathf.Abs(hit.forwardSlip) >= wheelCollider.forwardFriction.extremumSlip ||
                Mathf.Abs(hit.sidewaysSlip) >= wheelCollider.sidewaysFriction.extremumSlip
                )
            {
#pragma warning disable CS0618 // Type or member is obsolete
                wheelCollider.GetComponent <ParticleSystem>().enableEmission = true;
#pragma warning restore CS0618 // Type or member is obsolete
            }
            else
            {
#pragma warning disable CS0618 // Type or member is obsolete
                wheelCollider.GetComponent <ParticleSystem>().enableEmission = false;
#pragma warning restore CS0618 // Type or member is obsolete
            }
        }
        else
        {
            // no contact with ground, just extend wheel position with suspension distance

            localPosition = Vector3.Lerp(localPosition, -Vector3.up * collider.suspensionDistance, .05f);
#pragma warning disable CS0618 // Type or member is obsolete
            wheelCollider.GetComponent <ParticleSystem>().enableEmission = false;
#pragma warning restore CS0618 // Type or member is obsolete
        }

        // actually update the position

        wheelTransform.localPosition = localPosition;

        wheelTransform.localRotation = Quaternion.Euler(0, collider.steerAngle, 90);
    }
    private void FixedUpdate()
    {
        //BICYCLE MOVEMENT
        forwardAxis = Input.GetAxis("Vertical");
        turnAxis = Input.GetAxis("Horizontal");
        brakeAxis = Input.GetAxis("Jump");

        wheelFL.steerAngle = maxSteerAngle * turnAxis;
        wheelFR.steerAngle = maxSteerAngle * turnAxis;

        currentSpeed = 2 * 22 / 7 * wheelBL.radius * wheelBL.rpm * 60 / 100; //formula for calculating speed in rpm

        if (currentSpeed < topSpeed)
        {
            Debug.Log("Acelerando");
            wheelBL.motorTorque = maxTorque * forwardAxis; //run the wheel on the back
            wheelBR.motorTorque = maxTorque * forwardAxis;
        } //will not be accurate but will try to slow down the bike before top speed

        wheelFL.brakeTorque = maxBrakeTorque * brakeAxis;
        wheelFR.brakeTorque = maxBrakeTorque * brakeAxis;
        wheelBL.brakeTorque = maxBrakeTorque * brakeAxis;
        wheelBR.brakeTorque = maxBrakeTorque * brakeAxis;

        //BICYCLE ANTIROLL BARS
        WheelHit hit = new WheelHit();
        float travelL = 1f;
        float travelR = 1f;

        bool groundedL = wheelBL.GetGroundHit(out hit);
        if (groundedL)
            travelL = (-wheelBL.transform.InverseTransformPoint(hit.point).y - wheelBL.radius) / wheelBL.suspensionDistance;

        var groundedR = wheelBR.GetGroundHit(out hit);
        if (groundedR)
            travelR = (-wheelBR.transform.InverseTransformPoint(hit.point).y - wheelBR.radius) / wheelBR.suspensionDistance;

        var antiRollForce = (travelL - travelR) * antiRoll;

        if (groundedL)
            rb.AddForceAtPosition(wheelBL.transform.up * -antiRollForce, wheelBL.transform.position);

        if (groundedR)
            rb.AddForceAtPosition(wheelBR.transform.up * antiRollForce,
                   wheelBR.transform.position);
    }
示例#24
0
    //move wheels based on their suspension.

    void UpdateWheelPositions()

    {
        WheelHit contact = new WheelHit();

        if (fLeft.GetGroundHit(out contact))

        {
            Vector3 temp = fLeft.transform.position;

            //Note: trans.up works for my model, you might need trans.right if you rotated a cylinder!

            temp.y = (contact.point + (fLeft.transform.right * fLeft.radius)).y;

            wheelTransformFL.position = temp;
        }
        if (fRight.GetGroundHit(out contact))

        {
            Vector3 temp = fRight.transform.position;

            temp.y = (contact.point + (fRight.transform.right * fRight.radius)).y;

            wheelTransformFR.position = temp;
        }

        if (bLeft.GetGroundHit(out contact))

        {
            Vector3 temp = bLeft.transform.position;

            temp.y = (contact.point + (bLeft.transform.right * bLeft.radius)).y;

            wheelTransformBL.position = temp;
        }

        if (bRight.GetGroundHit(out contact))

        {
            Vector3 temp = bRight.transform.position;

            temp.y = (contact.point + (bRight.transform.right * bRight.radius)).y;

            wheelTransformBR.position = temp;
        }
    }
示例#25
0
    public void InitIfNull()
    {
        // Objects
        if (wheel == null)
        {
            wheel = new Wheel();
        }
        if (hit == null)
        {
            hit = new WheelHit();
        }
        if (spring == null)
        {
            spring = new Spring();
        }
        if (damper == null)
        {
            damper = new Damper();
        }
        if (fFriction == null)
        {
            fFriction = new Friction();
        }
        if (sFriction == null)
        {
            sFriction = new Friction();
        }

        // Curves
        if (springCurve == null)
        {
            springCurve = GenerateDefaultSpringCurve();
        }
        if (damperCurve == null)
        {
            damperCurve = GenerateDefaultDamperCurve();
        }
        if (sideFriction.frictionCurve == null)
        {
            sideFriction.frictionCurve = SetFrictionParams(sideFriction, new Vector4(10.0f, 1.9f, 1.0f, 0.97f));
        }
        if (forwardFriction.frictionCurve == null)
        {
            forwardFriction.frictionCurve = SetFrictionParams(forwardFriction, new Vector4(10.0f, 1.9f, 1.0f, 0.97f));
        }
    }
示例#26
0
文件: Car.cs 项目: Sarxdan/D0039D
    // Simulating stabilizer bars
    void StabilizeAxis(WheelCollider leftWheel, WheelCollider rightWheel)
    {
        WheelHit hit = new WheelHit();

        // Travel means the travel of the suspension
        float leftTravel  = 0.5f;
        float rightTravel = 0.5f;

        // Check if wheel is touching the ground
        bool leftGrounded = leftWheel.isGrounded;

        leftWheel.GetGroundHit(out hit);

        // Update travel if wheel is touching the ground
        if (leftGrounded)
        {
            leftTravel = (-leftWheel.transform.InverseTransformPoint(hit.point).y - leftWheel.radius) / leftWheel.suspensionDistance;
        }

        // Check if wheel is touching the ground
        bool rightGrounded = rightWheel.isGrounded;

        rightWheel.GetGroundHit(out hit);

        // Update travel if wheel is touching the ground
        if (rightGrounded)
        {
            rightTravel = (-rightWheel.transform.InverseTransformPoint(hit.point).y - rightWheel.radius) / rightWheel.suspensionDistance;
        }

        // Calculate the force to apply to each axis
        float antiRollForce = (leftTravel - rightTravel) * antiRollSpring;

        // Add force to wheels if wheel is grounded
        if (leftGrounded)
        {
            rigidbody.AddForceAtPosition(leftWheel.transform.up * -antiRollForce, leftWheel.transform.position);
        }
        // Add force to wheels if wheel is grounded
        if (rightGrounded)
        {
            rigidbody.AddForceAtPosition(rightWheel.transform.up * antiRollForce, rightWheel.transform.position);
        }
    }
示例#27
0
    void Update()
    {
        // Define a hit point for the raycase collision
        RaycastHit hit;
        // Find the collider's center point, you need to do this because the center of the collider might not actually
        // be the real position it the transform's off.
        Vector3 ColliderCenterPoint = colliderTransform.TransformPoint(CorrespondingCollider.center);

        // Now cast a ray out from the wheel collider's center the distance of the suspension, if it hit something, then use
        // the "hit", variable's data to find where the wheel hit, if it didn't, then set the wheel to be fully extended along
        // the suspension
        if (Physics.Raycast(ColliderCenterPoint, -colliderTransform.up, out hit, CorrespondingCollider.suspensionDistance + CorrespondingCollider.radius))
        {
            iTransform.position = hit.point + (colliderTransform.up * CorrespondingCollider.radius);
        }
        else
        {
            iTransform.position = ColliderCenterPoint - (colliderTransform.up * CorrespondingCollider.suspensionDistance);
        }

        // Now set the wheel rotation to the rotation of the collider comined with a new rotation value.
        // This new value is the roatation around the axle, and the rotation from steering input.
        iTransform.rotation = colliderTransform.rotation * Quaternion.Euler(RotationValue, CorrespondingCollider.steerAngle, 0);

        // Increase the rotation value by the rotation speed (in degrees per second)
        RotationValue += CorrespondingCollider.rpm * (360 / 60) * Time.deltaTime;

        // Define a whiell hit object, this stores all of the data from the wheel collider and will allow us to determine the slip
        // of the tire.

        WheelHit correspondingGroundHit = new WheelHit();

        CorrespondingCollider.GetGroundHit(out correspondingGroundHit);

        // If the slip of the tire is greater than 2.0f, and the slip prefab exists, create an instance of it on the ground a zero ratation.
        if (Mathf.Abs(correspondingGroundHit.sidewaysSlip) > SlipAmountForTireSmoke)
        {
            if (SlipPrefab)
            {
                SpawnController.Instance.Spawn(SlipPrefab, correspondingGroundHit.point, zeroRotation);
            }
        }
    }
    private void UpdateWheelFriction(WheelHit wheelHit)
    {
        WheelFrictionCurve sidewaysFrictionCurve = wheelsAll[0].sidewaysFriction;

        if (wheelHit.sidewaysSlip > maxSidewaysSlip || wheelHit.forwardSlip > maxForwardSlip)
        {
            sidewaysFrictionCurve.stiffness += frictionModifier;
        }
        else
        {
            sidewaysFrictionCurve.stiffness = tireSidewaysStiffnessDefault;
        }


        for (int i = 0; i < wheelsAll.Length; i++)
        {
            wheelsAll[i].sidewaysFriction = sidewaysFrictionCurve;
        }
    }
示例#29
0
    void Update()
    {
        if (runTimer)
        {
            timer += Time.deltaTime;
        }

        //rotate the wheels based on RPM
        float rotationThisFrame = 360 * Time.deltaTime;

        wheelTransformFL.Rotate(0, wheelFL.rpm / rotationThisFrame, 0);
        wheelTransformFR.Rotate(0, wheelFR.rpm / rotationThisFrame, 0);
        wheelTransformBL.Rotate(0, wheelBL.rpm / rotationThisFrame, 0);
        wheelTransformBR.Rotate(0, wheelBR.rpm / rotationThisFrame, 0);

//		if(wheelTransformFL.rotation.y < 10 && wheelTransformFL.rotation.y > -10)
//		{
//			Vector3 rot = new Vector3(wheelTransformFL.localEulerAngles.x,Input.GetAxis("Horizontal") * maxTurnAngle - wheelTransformFL.localEulerAngles.y,wheelTransformFL.localEulerAngles.z);
//			wheelTransformFL.localEulerAngles = rot;
//			wheelTransformFR.localEulerAngles = rot;
//		}

        //move wheels based on their suspension.
        WheelHit contact = new WheelHit();

        if (wheelFL.GetGroundHit(out contact))
        {
            wheelTransformFL.position = contact.point + (wheelFL.transform.up * wheelFL.radius);
        }
        if (wheelFR.GetGroundHit(out contact))
        {
            wheelTransformFR.position = contact.point + (wheelFR.transform.up * wheelFR.radius);
        }
        if (wheelBL.GetGroundHit(out contact))
        {
            wheelTransformBL.position = contact.point + (wheelBL.transform.up * wheelBL.radius);
        }
        if (wheelBR.GetGroundHit(out contact))
        {
            wheelTransformBR.position = contact.point + (wheelBR.transform.up * wheelBR.radius);
        }
    }
示例#30
0
        private IEnumerator StartSkidTrail(WheelHit wheelHit, Vector3 skidPoint)
        {
            // Vector3 normal = wheelHit.normal;
            // Vector3 newPos = skidPoint + normal * 0.1f;

            if (m_SkidTrail != null)
            {
                EndSkidTrail();
            }
            skidding = true;

            m_SkidTrail = Instantiate(SkidTrailPrefab);
            while (m_SkidTrail == null)
            {
                yield return(null);
            }

            m_SkidTrail.parent        = transform;
            m_SkidTrail.localPosition = -Vector3.up * m_WheelCollider.radius;
        }
示例#31
0
		public bool GetGroundHit(out WheelHit hit){}