private void WheelsSuspension() { for (int i = 0; i < Wheels.Length; i++) { isRayCast[i] = Physics.Raycast(Wheels[i].transform.position, Wheels[i].transform.TransformDirection(Vector3.down), out RayWheels[i], rayDistance, layerMask); if (isRayCast[i]) { Debug.DrawRay(Wheels[i].transform.position, Wheels[i].transform.TransformDirection(Vector3.down) * RayWheels[i].distance, Color.yellow); // World-space spring force direction Vector3 springDir = Wheels[i].up; // World-space tire velocity Vector3 tireWorldVel = carRB.GetPointVelocity(Wheels[i].position); // Offset from raycast float offset = suspensionRestDistance - RayWheels[i].distance; // suspensionRestDistance always < RayWheels[i].distance // Velocity along the spring direction float vel = Vector3.Dot(springDir, tireWorldVel); // Dampened spring force magnitude float force = (offset * springStrength) - (vel * springDamper); // Applyforce at tire location carRB.AddForceAtPosition(springDir * force, Wheels[i].position); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter