Determine change over time

Ok, I have tried to do my research to make this work for 3 days now and I need some help.

The end state = determine the revolutions per minute of a rotor blade.

I currently have a script that increments by 1 each time the blade passes 180 degrees.

I want to compare that value to determine the change per second. Any Ideas.

If anyone has a better solution to determine the RPM of a gameobject, I am all ears. Code is as follows:

using UnityEngine;
using System.Collections;

public class RotorRPM : MonoBehaviour
{



    private float totalRotation = 0;

    public int NumberofRotations
    {
        get
        {
            return ((int)totalRotation) / 360;
        }
    }
 
    public static int rotations;

    private Vector3 currentEuler;

    private Vector3 lastPoint;
    private int LastNumberofRotations;

    // Use this for initialization
    void Start()
    {
        lastPoint = transform.TransformDirection(Vector3.forward);
        lastPoint.y = 0;

        LastNumberofRotations = 0;
    }

    // Update is called once per frame
    void Update()
    {
        NumberOfRevolutions();
        rotations = NumberofRotations;
    }
    void NumberOfRevolutions()
    {
        Vector3 facing = transform.TransformDirection(Vector3.forward);
        facing.y = 0;

        float angle = Vector3.Angle(lastPoint, facing);
        if (Vector3.Cross(lastPoint, facing).y < 0)
            angle *= 1;

        totalRotation += angle;
        lastPoint = facing;
    }
}

Instead of trying to determine the RPM, why aren’t you using a defined RPM to control the rotation speed of the blades? It seems like a backwards way of doing it.

EDIT: If you insist on doing it that way, you can just check the angle the blades rotate in one frame, and multiply it up to a minute.

Something like this:

float RPM = (angle / 360) * (60 / Time.deltaTime);

Or, if you want it to be slightly faster :slight_smile:

float RPM = 1 / (6 * Time.deltaTime) * angle;

I tried what you suggested and it seems to print unreliable results. It may be due to my application. The object rotates quite fast and I think it my code cant measure the actual degrees of rotation in a frame.

The reason I do not use a defined speed is because I am driving the speed based on an animation. There are multiple states of the animation based on “engine” speed.

Thank you.

Could you post the code you use?

Put a rigidbody on it and check angular velocity.

I did not use a code to animate it. I used the animation controller. I suppose that after particular situations I could use code, but for example during start up, the animation starts from 0 to say 227 RPM. It starts off slower and progresses to 227 on a curve if that makes sense. Then progresses from 227 RPM to 351 RPM. Same concept. The acceleration from 227 to 351 is more constant, but I don’t even know where to begin on making a code for that!!

Can you just read the speed off of the curve?