Can't get things to animate properly

I’ve made a few animations for a gun object in an FPS I’m working on. This is the first time I’ve tried to use animations in blender. I’m pretty sure my animations themselves are ok, but my code that actually tells the gun when to use which animations doesn’t seem to work right. From what I read, I should simply be able to use “Animation.CrossFade(animation, time)” commands to phase in a new animation (and phase out the ones taht were previously playing. So if, for instance, my character is walking and the gun_walk animation is playing, and I suddenly stop walking, the gun should theoretically float back to the idle position over “time” seconds.

However the gun doesnt’ even move correctly when the character is walking. It will play the walk animation once when one of the movement keys is first pressed, then pause for a few seconds, then play the animation again, pause, play, and so on and so forth until the character is not moving anymore. Ok, so it sort of works, but why does the animation pause after each play? There are other problems too, most of which I don’t have the patience to try to describe in detail right now. I guess what I’m looking for is a general tutorial or suggestions on what commands would be used (and how they would be used) to animate a gun in a FPS and, more specifically, how to transition between animations so the gun doesn’t just jump from one position to another instantly every time a new animation runs on the gun object.

First off how do you have your animation files named, in my experience all separate animation files should be named as follows: gun01@idle, gun01@walk, gun01@shoot, etc (of course you will need to add your file extensions). Next do you have ‘animation wrap mode’ set to ‘loop’? If not set it in the inspector.

Here’s an example code of how to play various animations (untested mind you).

function Update () { 
	if (Input.GetKey("w")) {
		animation.CrossFade ("forward");
	} else {
		animation.CrossFade ("idle");
	}
	if (Input.GetKey("s")) {
		animation.CrossFade ("back");
	}
	if (Input.GetKey("a")) {
		animation.CrossFade ("left");
	}
	if (Input.GetKey("d")) {
		animation.CrossFade ("right");
	}
	if (Input.GetButton("Fire1")) {
		animation.CrossFade ("shoot");
	}
}

This should work, if not it may have something to do with the animation files.