Can I create a script that makes an animation advance 1 frame for each keystroke?

I have 86 views of my model that I need to be able to cycle through easily. So my camera has an animation that covers 86 keyframes, where each keyframe is a new view. Ideally, I could just press spacebar or something like that to advance one frame further, but so far it seems like all I can do is trigger the animation in its entirety. Any help would be much appreciated! Thanks.

OK guys sorry for making a new Answer, but i wanted to post this clearly and not hidden in a comment, i managed to make this work and it seems to work as the opener guy wanted ( and the same functionality i was needing )
Here is the updated, working code, in my case this goes on a Sun that rises and hides, by holding the DOWN key i’m advancing in the animation.

using UnityEngine;
using UnityEditor;
using System.Collections;

public class Sun : MonoBehaviour {
	public float animationSpeed = 1;
	private int currentKeyFrame = 0;
	private AnimationState animationState;
	void Start() {
		animationState = animation["sun_track"];
	}

	void Update() {
		// this stops the animation
		animationState.enabled = false;
		animationState.weight = 0;

		if(Input.GetKey(KeyCode.DownArrow))
		{
			nextKeyFrame();
		}
	}

	void nextKeyFrame()
	{
		animationState = animation["sun_track"];

		AnimationClipCurveData[] accd = AnimationUtility.GetAllCurves(animationState.clip, true);

		Debug.Log("keyframe: " + currentKeyFrame + " length: " + accd.Length);

		// this allows the animation to advance
		animationState.enabled = true;
		animationState.weight = 1;

		animationState.time += animationSpeed * Time.deltaTime;
	}
}

Although I haven’t tested it myself, I don’t see why you couldn’t do this. There are other posts out there that are related to this problem. For example, see here:

animation on keydown

or here:

accessing keyframes problem

So what I’d suggest is that you calculate how much TIME you want to pass (on key-down) rather than how many keyframes. Then you can simply loop through an animation from the beginning of the time chunk to the end of the time chunk that is represented by a key press.

You need to use AnimationState and AnimationCurve

int currentKeyFrame = 0;

void NextKeyFrame()
{
  animationState.time = animationCurve.keys[currentKeyFrame++].time;
}

Thanks guys! Forgive me-- I’m still very new to Unity but have some Java experience. Here’s the code I’m trying to run, and I’m getting the ‘expected ;’ error. Thanks.


function Update () {   
 	if (Input.GetKey(KeyCode.PageDown)) 
    	int currentKeyFrame = 0;
    	void NextKeyFrame()
    	{
    		animationState.time = animationCurve.keys[currentKeyFrame++].time;
    	}
    };