Search Unity

Calculating RPM

Discussion in 'Scripting' started by Timbecile76, Jan 18, 2012.

  1. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    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):
    1.         Vector3 targetPoint = new Vector3(myTarget.transform.position.x, transform.position.y, myTarget.transform.position.z);
    2.         // Determine the target rotation.  This is the rotation if the transform looks at the target point.
    3.         Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    4.        
    5.        
    6.         // Smoothly rotate towards the target point.
    7.         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?
     
  2. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    If one minute is 60 seconds and one revolution is 360 degrees, then every 6 degrees per second is 1RPM.
     
  3. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    that makes sense.

    so if I get the change in eulerAngles every second, and then multiply by 60 I should get the RPM?
     
  4. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    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.
     
  5. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    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):
    1.  
    2.     void CalculateRPM(){
    3.        
    4.         currentEuler = transform.rotation.eulerAngles;
    5.        
    6.         yDegreesPerSecond = Mathf.Abs(currentEuler.y - previousEuler.y);
    7.         gearRPM = 0.166666666667f * yDegreesPerSecond;
    8.         previousEuler = currentEuler;
    9.     }
    10.  
    I probably can truncate the 0.166666666667f to something simpler like 0.167 or something.....
     
    harleydk likes this.
  6. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Leave it there, the more precise the better it is.
     
  7. EricMetaverse

    EricMetaverse

    Joined:
    May 9, 2022
    Posts:
    1
    Code (CSharp):
    1. [SerializeField] List<WheelCollider> allWheels;
    2. int GetWheelRpm() {
    3.         int count = 0;
    4.         foreach(WheelCollider wheel in allWheels) {
    5.             count += Mathf.RoundToInt(Mathf.Abs(wheel.rpm));
    6.         }
    7.         return Mathf.RoundToInt(count / allWheels.Count);
    8. }
    docs.unity3d.com/ScriptReference/WheelCollider-rpm.html
     
    NihadProgramer likes this.