ok, been trying to wrap my brain around this for a while, and I'm still stuck. I've got a gear that's being rotated to follow the mouse cursor with this code: Code (csharp): Vector3 targetPoint = new Vector3(myTarget.transform.position.x, transform.position.y, myTarget.transform.position.z); // Determine the target rotation. This is the rotation if the transform looks at the target point. Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position); // Smoothly rotate towards the target point. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime); I also need to calculate the RPM while the player is rotating the gear. (it could change at any given time, since the player is rotating the gear) Math is not my strong point. anyone have any suggestions on how to calculate RPM?
If one minute is 60 seconds and one revolution is 360 degrees, then every 6 degrees per second is 1RPM.
that makes sense. so if I get the change in eulerAngles every second, and then multiply by 60 I should get the RPM?
Yup. But remember your ticks are a fraction of a second, so you will need that same fraction of 6 degrees. You might want to grab the total rotation on a fixed schedule and do some fine tuning, but that should get you started.
So I found a site that gave me the following formula: RPM = degrees/sec * 0.166666666667 so I think I've got it working now. Here's my RPM function (it calculates RPM on the Y axis only): Code (csharp): void CalculateRPM(){ currentEuler = transform.rotation.eulerAngles; yDegreesPerSecond = Mathf.Abs(currentEuler.y - previousEuler.y); gearRPM = 0.166666666667f * yDegreesPerSecond; previousEuler = currentEuler; } I probably can truncate the 0.166666666667f to something simpler like 0.167 or something.....
Code (CSharp): [SerializeField] List<WheelCollider> allWheels; int GetWheelRpm() { int count = 0; foreach(WheelCollider wheel in allWheels) { count += Mathf.RoundToInt(Mathf.Abs(wheel.rpm)); } return Mathf.RoundToInt(count / allWheels.Count); } docs.unity3d.com/ScriptReference/WheelCollider-rpm.html