I need help writing an animation script for a Bow and Arrow weapon

I’m having trouble getting the proper animation to play for my game. It is a first person shooter where the players weapon is a bow. I currently have it set up so that all you need are two animations. The idle animation and the shoot/reload animation.

I am trying to get a script written where the bow is ready to shoot (which is the idle animation) and when the player clicks, the arrow is shot and the character in game reloads the bow and gets the arrow ready for another shot. I have all the components working for the shooting and fire delay. I just need to understand how to get the animations working. I have little no to experience in scripting, but I was able to get this (by looking through tutorials and manuals) so far. I know its not right, but it’s what I have so far.

var Idle   : AnimationClip;
var Shoot  : AnimationClip;

function Update ()

{
   if (Input.GetAxis("Fire1"))
   animation.Play("Shoot");
   else
   animation.Play("Idle");
} 

Using this code, the animation will play, but only while the mouse button is held down. As soon as I let go, the animation snaps back to its idle. How would I get the animation to play through until its finished? And also have it so that when the player clicks again, it doesn’t restart the animation? Thank you in advance.

1
I would use cross-fade instead of play to make the transitions look better.

2
I would use coroutines and wait after triggering the “shoot” animation (for the duration of the animation), then trigger the “idle” animation.

3
Before triggering any animation, you can query the animation component about the current animation. This way you can avoid restarting.

For all this, you could also use behaviour trees (there is the behave software from angry ant), but if you have little scripting experience, the concepts may be a little too advanced. While your gameplay mechanics stay simple you will probably do fine without it. Maybe it is still worth looking into for you. :slight_smile: