Initiate Function only on certain frames?

I’m not quite sure the best way to approach this…I have an object with an animation that spans 24 frames, and i need a way for an Input.GetButtonDown associated with this object to only function between frame 7,8,9
any idea how i’d go about doing this?

you could make you a little fct to get your anim time in second equivalent to you frame.

using the framerate of your anim and the total frame number you have.

and then form that check if you are in the right time range or frame.

I use something like this to trigger sprite FX at some specific time / frame in my player anim.

i think i have an idea of what you mean, but my lack of technical skills are setting me back. Do you have any example code you could post?

You could try a Coroutine Solution:

	int numFrames = 3;
	
	//Place this function call within your animation timeline at frame 7
	public void AnimEvent()
	{
		StartCoroutine(GetInputs());
	}
	
	IEnumerator GetInputs()
	{
		int i = 0;
		while(i < numFrames)
		{
			GetInput();
         i++;
			yield return 0;	
		}
	}
	
	public void GetInput()
	{
		//Place your Input Polling Here	
	}

Using
http://unity3d.com/support/documentation/Components/animeditor-AnimationEvents.html
add a function call to AnimEvent() on frame 7.

Now you can have the Input polling run for numFrames number of frames. It allows you to reuse it for times when you want a new number of frames to be checked during.

Unity uses time instead of frames, which makes sense since you’re not guaranteed for things to land exactly on a frame. Basically it means you have an input window.

Assuming your animation is playing at 30 frames per second, your input window is going to be between 0.23 seconds and 0.3 seconds, so just use something like this in an update function :

if (animation["yourAnim"].time > 0.23  animation["yourAnim"].time < 0.3) {
    if (Input.GetButtonDown("yourButton")) {
        //stuff that happens when button is pressed between frames 7 and 9 goes here
    }
}

I would divide the animation into 3 parts.

part 1: frames 1-6
part 2: frames 7-9
part 3: frames 10-24

Then I’d do something like this:

Play part 1

When part 1 is done, see if Input.GetButtonDown is true already. If so, the user has the mouse button down currently. b_already_clicking=true. Else, b_already_clicking = false.

Play part 2
During this part, if b_already_clicking=false and Input.GetButtonDown=true then they clicked at the right time. :smile:
If b_already_clicking=true and Input.GetButtonDown=false they they just released the mouse button, so b_already_clicking=false. (You should be running this during Update or FixedUpdate so you can register whether the player clicks after releasing the mouse.)

When part 2 is done, play part 3. Ignore Input.GetButtonDown.

If you need to loop, just play part 1 again.