- Home /
repeating a shooting animation
I'm trying to put together a simple set of animations on a third person character. At the moment I have an idle, a walk, and a shoot and reload. The shoot and reload are blended on top of the other animations, so the character can shoot/ reload and walk/ idle.
What I would like to know - is there a way to get the shoot animation to keep on playing with the mouse button pressed down? At the moment I have to release and repress to get the shoot animation to play again.
If I change GetButtonDown to GetButton this can repeat but this doesn't work with the mixing and crossfade. If I change the animation to play rather than crossfade then the mixing looks awful on the character (I'm using the first spine joint as the mixer root) but the animation repeats until the mouse button is released.
Any way to have the animation blending/ mixing nicely and being able to hold the mouse button down to carry on firing?
(Eventally I will have a "bring to firing position/ one shot fire" animation and then a "repeat firing" animation, but that's a way off yet(.
Here is my current script:
/// Adds a mixing transform using a Transform variable public var shoulder : Transform; animation["shoot"].AddMixingTransform(shoulder); animation["reload"].AddMixingTransform(shoulder);
private var leanLeft : AnimationState; private var leanRight : AnimationState;
function Start () {
// Set all animations to loop animation.wrapMode = WrapMode.Loop; // except shooting animation["shoot"].wrapMode = WrapMode.Once; animation["reload"].wrapMode = WrapMode.Once;
// Put idle and walk into lower layers (The default layer is always 0) // This will do two things // - Since shoot and idle/walk are in different layers they will not affect // each other's playback when calling CrossFade. // - Since shoot is in a higher layer, the animation will replace idle/walk // animations when faded in. animation["shoot"].layer = 1; animation["reload"].layer = 2;
// Stop animations that are already playing //(In case user forgot to disable play automatically) animation.Stop();
leanLeft = animation["leanLeft"];
leanRight = animation["leanRight"];
// Put the leaning animation in a separate layer // So that other calls to CrossFade won't affect it. leanLeft.layer = 10; leanRight.layer = 10;
// Set the lean animation to be additive leanLeft.blendMode = AnimationBlendMode.Additive; leanRight.blendMode = AnimationBlendMode.Additive;
// Set the lean animation ClampForever // With ClampForever animations will not stop // automatically when reaching the end of the clip leanLeft.wrapMode = WrapMode.ClampForever; leanRight.wrapMode = WrapMode.ClampForever;
// Enable the animation and fade it in completely // We don't use animation.Play here because we manually adjust the time // in the Update function. // Instead we just enable the animation and set it to full weight leanRight.enabled = true; leanLeft.enabled = true; leanRight.weight = 1.0; leanLeft.weight = 1.0;
// For testing just play "walk" animation and loop it //animation["walk"].wrapMode = WrapMode.Loop; //animation.Play("walk");
}
function Update () { // Play either the run or idle animation if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1) { animation.CrossFade("walk"); // Play animation backwards when running backwards animation["walk"].speed = Mathf.Sign(Input.GetAxis("Vertical")); } else animation.CrossFade("idle");
// Shoot if (Input.GetButtonDown ("Fire1")) animation.CrossFade ("shoot");
if (Input.GetButtonDown ("reload")) animation.CrossFade("reload");
// Every frame just set the normalized time
// based on how much lean we want to apply
var lean = Input.GetAxis("Horizontal"); // normalizedTime is 0 at the first frame and 1 at the last frame in the clip leanLeft.normalizedTime = -lean; leanRight.normalizedTime = lean;
}
Thanks
Answer by kyle83great · Jun 21, 2012 at 10:40 PM
im only a 13 year old teen...but i do know how to do loop
animation["shoot"].wrapMode = WrapMode.Once;
rather then that then use
animation["shoot"].wrapMode = WrapMode.Loop;