//alligns the Wheel meshes with the position and rotation of the wheel colliders public void SetWheelMeshes(WheelSet wheelSet) { Quaternion rotation; Vector3 position; wheelSet.rightWheelCollider.GetWorldPose(out position, out rotation); wheelSet.rightWheel.transform.position = position; wheelSet.rightWheel.transform.rotation = rotation; wheelSet.leftWheelCollider.GetWorldPose(out position, out rotation); wheelSet.leftWheel.transform.position = position; wheelSet.leftWheel.transform.rotation = rotation; }
//transfer some compression force from one spring to the opposite on same axle public void Stabilize(WheelSet wheelSet) { WheelHit hit; //travel values between 0.0 (fully compressed) and 1.0 (fully extended) //if not on the ground must be fully extended float travelRight = 1.0f; float travelLeft = 1.0f; bool groundedRight = wheelSet.rightWheelCollider.GetGroundHit(out hit); if (groundedRight) { //find the y coordinate of the hit point in world space travelRight = (-wheelSet.rightWheelCollider.transform.InverseTransformDirection(hit.point).y - wheelSet.rightWheelCollider.radius) / wheelSet.rightWheelCollider.suspensionDistance; } else { wheelSet.rightWheelCollider.motorTorque = 0; } bool groundedLeft = wheelSet.leftWheelCollider.GetGroundHit(out hit); if (groundedLeft) { travelLeft = (-wheelSet.leftWheelCollider.transform.InverseTransformDirection(hit.point).y - wheelSet.leftWheelCollider.radius) / wheelSet.leftWheelCollider.suspensionDistance; } else { wheelSet.leftWheelCollider.motorTorque = 0; } //antiRollForce to be transfered depends on difference in suspension float antiRollForce = (travelLeft - travelRight) * antiRoll; //subtract the force from one spring and add it to the other if (groundedRight) { wheelSet.rightWheelCollider.attachedRigidbody.AddForceAtPosition(wheelSet.rightWheelCollider.transform.up * (antiRollForce * -1), wheelSet.rightWheelCollider.transform.position); } if (groundedLeft) { wheelSet.leftWheelCollider.attachedRigidbody.AddForceAtPosition(wheelSet.leftWheelCollider.transform.up * (antiRollForce * -1), wheelSet.leftWheelCollider.transform.position); } }
// --------------------------- Start --------------------------------------------------------------------- public void Start() { foreach (Transform child in transform) { if(child.gameObject.name.Contains("wheel")){ if (wheels == null) wheels = new List<WheelSet>(); WheelSet wheel=new WheelSet(); if(child.gameObject.name.Contains("f")){ wheel.steered = true; } wheel.wheelgraphic=child; wheel.powered = true; wheel.handbraked = true; wheel.wheelaxle = child; wheels.Add(wheel); } } // wheels enumerator if (wheels != null) { WheelsN = wheels.Count; foreach (WheelSet w in wheels) { w.originalRotation = w.wheelgraphic.rotation; w.originalBrakeRotation = w.wheelaxle.rotation; //create collider GameObject colliderObject = new GameObject("WheelC"); colliderObject.transform.parent = transform; colliderObject.transform.position = w.wheelgraphic.position; w.wCollider = colliderObject.AddComponent<WheelCollider>(); w.wCollider.suspensionDistance = springLength; JointSpring springSetup = new JointSpring(); springSetup.spring = springForce; springSetup.damper = damperForce; w.wCollider.suspensionSpring = springSetup; WheelFrictionCurve forFrictionSetup = new WheelFrictionCurve(); WheelFrictionCurve sideFrictionSetup = new WheelFrictionCurve(); forFrictionSetup.stiffness = FrontSlip; sideFrictionSetup.stiffness = SideSlip; w.wCollider.forwardFriction = forFrictionSetup; w.wCollider.sidewaysFriction = sideFrictionSetup; w.wCollider.mass = WheelMass; w.wCollider.radius = WheelRadius; if (w.powered) { MWheelsN++; } CurrentGear = 0; } // mass center adjustment rigidbody.centerOfMass = massCorrection; Debug.Log(rigidbody.centerOfMass); } else { Debug.LogError("No wheels assigned!"); } if (MWheelsN == 0) { Debug.Log("No motor wheels assigned!"); } }