Execute on one frame using a float - Been looking for a solution for a while now...

So for example. I use the right trigger on the xbox controller. It works with a float from 0 to 1. When it’s set to 1 (fully pushed in). I want to do something for and on only 1 frame. So I guess that would be the next frame and only the next frame.

I think I’ll have to use a coroutine but I understand a coroutine also needs to be triggered to work. I’m using function update and in my experience it’s impossible since update runs once every frame.

I have no idea how to pull this off, have been looking for a year how to execute something on 1 frame only. I have been using invoke() but it’s imo a bad & imprecise solution to this problem.

I need a thorough and simple explanation please.

Using Input.GetKeyDown results in using only one frame. I need a result like this. But I’m using a float-based button in this case.

It’s solved.

With this solution I was communicating between scripts causing discrepancies of 1 to 3 frames long. Now that I’m adapting this solution directly it works and only 1 frame is accounted for.

Here’s a code example for anyone interested:

//Declaration
bool Trig = True;
float Rxboxtrigger;

void Update() {

Rxboxtrigger = Input.GetAxis ("[TheRightXboxTrigger]");

if (Trig == True) {
       if (Rxboxtrigger == 1f) {
            //Do something
            //
            //
            Trig = false; 
            }
      }
if (Rxboxtrigger < 1f) {
      Trig = true;
      }
}

Further explanation: Basically use the false state of a bool and since the bool is set to false later whilst the Rxboxtrigger is still set to 1, it won’t execute again since Trig is set to false after we ‘DoSomething’… Trig gets reset when you release the trigger leaving its 1 state.