void FixedUpdate() { WheelHit groundHit; wheel_col.GetGroundHit(out groundHit); float wheelSlipAmount = Mathf.Abs(groundHit.sidewaysSlip); if (wheelSlipAmount > startSlipValue) //if sideways slip is more than desired value { Vector3 skidPoint = groundHit.point + 2 * (skidCaller.rigidbody.velocity) * Time.deltaTime; lastSkidmark = skidmarks.AddSkidMark(skidPoint, groundHit.normal, wheelSlipAmount / 25.0F, lastSkidmark); } else { lastSkidmark = -1; } }
void DoGetGroundHit() { if (_wheel == null) { return; } _wheel.GetGroundHit(out _wheelhit); if (!collider.IsNone) { if (_wheelhit.collider != null) { collider.Value = _wheelhit.collider.gameObject; } else { UnityEngine.Debug.LogWarning("Missing Collider in wheelhit data"); } } if (!point.IsNone) { point.Value = _wheelhit.point; } if (!normal.IsNone) { normal.Value = _wheelhit.normal; } if (!forwardDir.IsNone) { forwardDir.Value = _wheelhit.forwardDir; } if (!force.IsNone) { force.Value = _wheelhit.force; } if (!forwardSlip.IsNone) { forwardSlip.Value = _wheelhit.forwardSlip; } if (!sidewaysSlip.IsNone) { sidewaysSlip.Value = _wheelhit.sidewaysSlip; } }
void Update() { Vector3 colliderCenter = wheelCollider.transform.TransformPoint(wheelCollider.center); if (wheelCollider.GetGroundHit(out hit)) { transform.position = hit.point + (wheelCollider.transform.up * wheelCollider.radius); } else { transform.position = colliderCenter - (wheelCollider.transform.up * wheelCollider.suspensionDistance); } transform.rotation = wheelCollider.transform.rotation * Quaternion.Euler(rotation, wheelCollider.steerAngle, 0); rotation += wheelCollider.rpm * (360 / 60) * Time.deltaTime; }
//检测打滑 private void DetectSkid() { WheelHit hit; m_WheelCollider.GetGroundHit(out hit); if (Mathf.Abs(hit.sidewaysSlip) >= slipLimit && m_WheelCollider.attachedRigidbody.velocity.magnitude > 3f) { PlayAudio(); EmitSmokeAndTrail(); } else { //没有打滑时停止效果 StopAudio(); EndSkidTrail(); } }
void FixedUpdate() { if (GameConfigs.isUIRunning) { return; } if (!GameConfigs.isGameRunning) { return; } /* * float force = (wheelL.compression - wheelR.compression) * coefficient; * wheelL.suspensionForceInput =+ force; * wheelR.suspensionForceInput =- force; */ 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) { rigidbody.AddForceAtPosition(wheelL.transform.up * -antiRollForce, wheelL.transform.position); } if (groundedR) { rigidbody.AddForceAtPosition(wheelR.transform.up * antiRollForce, wheelR.transform.position); } }
private void EnableGrassParticles(WheelCollider wheel, ParticleSystem grassParticles) { WheelHit hit; if (wheel.GetGroundHit(out hit)) { if (hit.collider.name == "Grass" && verticalInput > 0) { if (!grassParticles.isPlaying) { grassParticles.Play(); } return; } } grassParticles.Stop(); }
public void checkLanding() { if (!HasUsableWheel()) { return; } if (moduleWheel.isGrounded && !wheelHasWeight && part.vessel.srfSpeed > 10) { ImpactSounds(true); WheelHit hit; if (wheelCollider.GetGroundHit(out hit)) { DustImpact((float)part.vessel.srfSpeed, hit.point, hit.collider.name); } } wheelHasWeight = moduleWheel.isGrounded; }
// 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); } }
void CheckWheel() { for (int i = 0; i < 4; i++) { WheelCollider wheel = wc[i]; WheelHit hit; if (wheel.GetGroundHit(out hit)) { if (Mathf.Abs(hit.forwardSlip) > 0.5 || Mathf.Abs(hit.sidewaysSlip) > 0.5) { wheelMonitor[i].color = Color.red; } else { wheelMonitor[i].color = new Color(1f, 1 - Mathf.Abs(hit.forwardSlip), 1 - Mathf.Abs(hit.sidewaysSlip)); } } } }
void AntiRoll(ref WheelCollider WheelL, ref WheelCollider WheelR, float AntiRoll) { // Source code taken from the website listened below // // http://forum.unity3d.com/threads/how-to-make-a-physically-real-stable-car-with-wheelcolliders.50643/ // // http://www.edy.es/dev/vehicle-physics/live-demo // WheelHit hit; float travelL = 1.0f; float travelR = 1.0f; bool groundedL = WheelL.GetGroundHit(out hit); if (groundedL) { if (hit.collider.transform.name == "StartLine") { Debug.Log("Hit the StartLine"); } 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; } var antiRollForce = (travelL - travelR) * AntiRoll; if (groundedL) { rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce, WheelL.transform.position); } if (groundedR) { rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position); } }
//During collision private void OnCollisionStay(Collision collision) { WheelHit hit; frontDriverC = frontDriverW.GetGroundHit(out hit); frontPassengerC = frontPassengerW.GetGroundHit(out hit); rearDriverC = rearDriverW.GetGroundHit(out hit); rearPassengerC = rearPassengerW.GetGroundHit(out hit); //Checks if all wheels are touching the ground if (frontDriverC && frontPassengerC && rearDriverC && rearPassengerC) { allWheelsOnGround = true; } else { allWheelsOnGround = false; } }
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); } } }
protected void LateUpdate() { if (wheelCollider.GetGroundHit(out wheelHitInfo)) { // Check sideways speed // Gives velocity with +z being the car's forward axis Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity); float skidTotal = Mathf.Abs(localVelocity.x); // Check wheel spin as well float wheelAngularVelocity = wheelCollider.radius * ((2 * Mathf.PI * wheelCollider.rpm) / 60); float carForwardVel = Vector3.Dot(rb.velocity, transform.forward); float wheelSpin = Mathf.Abs(carForwardVel - wheelAngularVelocity) * WHEEL_SLIP_MULTIPLIER; // NOTE: This extra line should not be needed and you can take it out if you have decent wheel physics // The built-in Unity demo car is actually skidding its wheels the ENTIRE time you're accelerating, // so this fades out the wheelspin-based skid as speed increases to make it look almost OK wheelSpin = Mathf.Max(0, wheelSpin * (10 - carForwardVel)); skidTotal += wheelSpin; // Skid if we should if (skidTotal >= SKID_FX_SPEED) { float intensity = Mathf.Clamp01(skidTotal / MAX_SKID_INTENSITY); // Account for further movement since the last FixedUpdate Vector3 skidPoint = wheelCollider.transform.position; // - wheelCollider.transform.up * wheelCollider.radius;//wheelHitInfo.point;// + (rb.velocity * (Time.time - lastFixedUpdateTime)); // Fixed Skidmarks being placed to low in line above -- Paal lastSkid = skidmarksController.AddSkidMark(skidPoint, wheelHitInfo.normal, intensity, lastSkid); } else { lastSkid = -1; } } else { lastSkid = -1; } }
void Update() { if (simpleCarScript.superSimplePhysics) { CorrespondingCollider.enabled = false; this.enabled = false; return; } pos1 = wheelTransform.position; up = wheelTransform.up; WheelHit CorrespondingGroundHit; bool grounded = CorrespondingCollider.GetGroundHit(out CorrespondingGroundHit); //Check if the wheel is grounded and set the compresion value accordingly if (grounded) { _compression = 1.0f - ((Vector3.Dot(pos1 - CorrespondingGroundHit.point, up) - radius) / suspensionTravel); } else { _compression = suspensionTravel; } //Set the local position of the transform myTransform.localPosition = (Vector3.up * (_compression - 1.0f) * suspensionTravel); //Set the rotation of the transform myTransform.rotation = CorrespondingCollider.transform.rotation * Quaternion.Euler(RotationValue, CorrespondingCollider.steerAngle, 0); //Increase the rotation value RotationValue += CorrespondingCollider.rpm * (360f / 60f) * Time.deltaTime; //Check if we are sliding on the sides if (Mathf.Abs(CorrespondingGroundHit.sidewaysSlip) > 2.0f) { if (SlipPrefab) { Instantiate(SlipPrefab, CorrespondingGroundHit.point, Quaternion.identity); } } }
// Update is called once per frame public virtual void LateUpdate() { encoderVal += wheel.rpm * Time.deltaTime; bool hitGround = wheel.GetGroundHit(out hit); //print(hit.forwardSlip); if (targetForce == 0) { wheel.brakeTorque = brakeForce; } else { wheel.brakeTorque = 0; } force = Mathf.Lerp(force, targetForce, 50 * Time.deltaTime); wheel.motorTorque = Mathf.Lerp(2 * forceMult * targetForce, 0, Mathf.Abs(wheel.rpm) / 600); }
//UPDATE SKIDMARKS PER WHEEL PASSED void skidmarks(WheelCollider coll) { // define a wheelhit object, this stores all of the data from the wheel collider and will allow us to determine // the slip of the tire. WheelHit CorrespondingGroundHit; coll.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 at // a zero rotation. if (Mathf.Abs(CorrespondingGroundHit.sidewaysSlip) > .6) { //Instantiate (skidMarkPrefab, CorrespondingGroundHit.point, coll.transform.rotation); Debug.Log("skidding"); } else if (Mathf.Abs(CorrespondingGroundHit.sidewaysSlip) <= .45) { //skidMarkPrefab.gameObject.SetActive(false); } }
//Alligns the wheels position from the wheel collider private void FixedUpdate() { Vector3 position; Quaternion rotation; //Get the collider's position and rotation wheelCollider.GetWorldPose(out position, out rotation); wheelModel.position = new Vector3(position.x, position.y, position.z); wheelModel.transform.rotation = rotation; WheelHit hit; //Applies friction to wheels if (wheelCollider.GetGroundHit(out hit)) { PhysicMaterial material = hit.collider.material; WheelFrictionCurve forwardCurve = wheelCollider.forwardFriction; forwardCurve.stiffness = material.dynamicFriction; wheelCollider.forwardFriction = forwardCurve; } }
void FixedUpdate() { WheelHit GroundHit; wheel_col.GetGroundHit(out GroundHit); var wheelSlipAmount = Mathf.Abs(GroundHit.sidewaysSlip); if (wheelSlipAmount > startSlipValue) { Vector3 skidPoint = GroundHit.point + 2 * (carParent.velocity) * Time.deltaTime; lastSkidmark = skidmarks.AddSkidMark(skidPoint, GroundHit.normal, wheelSlipAmount / 2.0f, lastSkidmark, markWidth); //skidmarks.PlaySkidSound (true); } else { lastSkidmark = -1; ///skidmarks.PlaySkidSound (false); } }
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 ) { //wheelCollider.GetComponent<ParticleSystem>().enableEmission = true; } else { //wheelCollider.GetComponent<ParticleSystem>().enableEmission = false; } } else { // no contact with ground, just extend wheel position with suspension distance localPosition = Vector3.Lerp(localPosition, -Vector3.up * collider.suspensionDistance, .05f); //wheelCollider.GetComponent<ParticleSystem>().enableEmission = false; } // actually update the position wheelTransform.localPosition = localPosition; wheelTransform.localRotation = Quaternion.Euler(0, collider.steerAngle, 0); }
private void Update() { // define a hit point for the raycast 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 if the transform's off. Vector3 ColliderCenterPoint = CorrespondingCollider.transform.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 se tthe wheel to be fully extended along the suspension. if (Physics.Raycast(ColliderCenterPoint, -CorrespondingCollider.transform.up, out hit, CorrespondingCollider.suspensionDistance + CorrespondingCollider.radius)) { transform.position = hit.point + (CorrespondingCollider.transform.up * CorrespondingCollider.radius); } else { transform.position = ColliderCenterPoint - (CorrespondingCollider.transform.up * CorrespondingCollider.suspensionDistance); } // now set the wheel rotation to the rotation of the collider combined with a new rotation value. This new value // is the rotation around the axle, and the rotation from steering input. transform.rotation = CorrespondingCollider.transform.rotation * Quaternion.Euler(RotationValue, CorrespondingCollider.steerAngle, 0); // increase the rotation value by the rotation speed (in degrees per second) RotationValue += CorrespondingCollider.rpm * (360 / 60f) * Time.deltaTime; // define a wheelhit object, this stores all of the data from the wheel collider and will allow us to determine // the slip of the tire. WheelHit CorrespondingGroundHit; CorrespondingCollider.GetGroundHit(out CorrespondingGroundHit); // if the slip of the tire is greater than 2.0, and the slip prefab exists, create an instance of it on the ground at // a zero rotation. if (Mathf.Abs(CorrespondingGroundHit.sidewaysSlip) > 1.5f) { if (SlipPrefab) { Instantiate(SlipPrefab, CorrespondingGroundHit.point, Quaternion.identity); } } }
// Update is called once per frame void Update() { WheelHit hit = new WheelHit(); WheelCollider wheel = GetComponent <WheelCollider>(); if (wheel.GetGroundHit(out hit)) { if (hit.forwardSlip < -1 || hit.sidewaysSlip < -0.5 || hit.sidewaysSlip > 0.5) { StartSmoke(); } else { StopSmoke(); } } else { StopSmoke(); } }
void FixedUpdate() { WheelHit hit = new WheelHit(); if (wheel.GetGroundHit(out hit)) { float hitY = wheel.transform.InverseTransformPoint(hit.point).y; float speed = bus.velocity.sqrMagnitude; Vector3 direction = bus.velocity.normalized; if (Mathf.Abs(hit.forwardSlip) >= wheel.forwardFriction.extremumSlip || Mathf.Abs(hit.sidewaysSlip) >= wheel.sidewaysFriction.extremumSlip) { emission.rateOverTime = 20 + (50 * Random.value); velocity.xMultiplier = -direction.x * speed * speedFactor; velocity.zMultiplier = -direction.z * speed * speedFactor; } else { emission.rateOverTime = 0; } } }
void wheelToCol() { float delta = Time.fixedDeltaTime; WheelHit hit; Vector3 lp = transform.localPosition; if (wheelCol.GetGroundHit(out hit)) { lp.y -= Vector3.Dot(transform.position - hit.point, car.transform.up) - wheelCol.radius; } else { lp.y = wheelStartPos.y - wheelOffset; } transform.localPosition = lp; rotation = Mathf.Repeat(rotation + delta * wheelCol.rpm * 360.0f / 60.0f, 360.0f); transform.localRotation = Quaternion.Euler(rotation, wheelCol.steerAngle, 0.0f); }
private bool GetNormal(out Vector3 normal) { WheelHit hit; Vector3 vect = Vector3.zero; bool isGrounded = false; if (backWheel.GetGroundHit(out hit)) { vect += hit.normal; isGrounded = true; } if (frontWheel.GetGroundHit(out hit)) { vect += hit.normal; isGrounded = true; } normal = vect; return(isGrounded); }
void FixedUpdate() { WheelHit hit; if (m_WheelCollider.GetGroundHit(out hit)) { if (hit.collider.gameObject.tag == "Bump") { if (GameObject.Find("Car").GetComponent <CarController>().CurrentSpeed > 15f) { Debug.Log("Speed AWi"); GameObject.Find("Car").GetComponent <CarRemoteControl>().crash = true; } else { Debug.Log("Speed Wati"); GameObject.Find("Car").GetComponent <CarRemoteControl>().crash = false; } } } }
void Update() { WheelHit hit; if (attachedWheel.GetGroundHit(out hit)) { float frictionValue = Mathf.Abs(hit.sidewaysSlip); if (skidAt <= frictionValue && soundDelay <= 0) { AudioSource.PlayClipAtPoint(skidSound, hit.point); soundDelay = 1f; } } soundDelay -= Time.deltaTime * soundEmissionPerSecond; }
void FixedUpdate() { if (Time.realtimeSinceStartup - lastUpdate < 1f / 60f) { return; } lastUpdate = Time.realtimeSinceStartup; if (_wheelModel && _wheelCollider) { Vector3 pos = new Vector3(0, 0, 0); Quaternion quat = new Quaternion(); _wheelCollider.GetWorldPose(out pos, out quat); _wheelModel.transform.rotation = quat; _wheelModel.transform.localRotation *= Quaternion.Euler(localRotOffset); _wheelModel.transform.position = pos; WheelHit wheelHit; _wheelCollider.GetGroundHit(out wheelHit); } }
void DoRollBar(WheelCollider WheelL, WheelCollider WheelR) { WheelHit hit; float travelL = 1.0f; float travelR = 1.0f; 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; }
private WheelHit GetGroundInfos(ref WheelCollider wheelCol, ref string groundTag, ref int groundTextureIndex) { // Default values groundTag = "InTheAir"; groundTextureIndex = -1; // Query ground by ray shoot on the front left wheel collider WheelHit wheelHit; wheelCol.GetGroundHit(out wheelHit); // If not in the air query collider if (wheelHit.collider) { groundTag = wheelHit.collider.tag; if (wheelHit.collider.CompareTag("Terrain")) { groundTextureIndex = TerrainSurface.GetMainTexture(transform.position); } } return(wheelHit); }
public void SetMotorTorque(float newThrottle, bool rightSide, float torqueAmount) { int i; WheelHit wheelHit; WheelHit wheelHit1; newThrottle = Mathf.Clamp(newThrottle, -1f, 1f); float single = torqueAmount * newThrottle; int num = (rightSide ? (int)this.rightWheels.Length : (int)this.leftWheels.Length); int num1 = 0; WheelCollider[] wheelColliderArray = (rightSide ? this.rightWheels : this.leftWheels); for (i = 0; i < (int)wheelColliderArray.Length; i++) { if (wheelColliderArray[i].GetGroundHit(out wheelHit)) { num1++; } } float single1 = 1f; if (num1 > 0) { single1 = (float)(num / num1); } wheelColliderArray = (rightSide ? this.rightWheels : this.leftWheels); for (i = 0; i < (int)wheelColliderArray.Length; i++) { WheelCollider wheelCollider = wheelColliderArray[i]; if (!wheelCollider.GetGroundHit(out wheelHit1)) { wheelCollider.motorTorque = single; } else { wheelCollider.motorTorque = single * single1; } } }