When G key huh? Well first you need to assign that G key an attribute, such as naming it the attack button. To do that, you need to go into your Project Settings (Edit > Project Settings > Input) and create a new key that reacts to pressing the G key which will be named your attack button.
Now, using snippets of my own animation scripts, your script should look similar:
function Start ()
{
controller = GetComponent(CharacterController);
animation.wrapMode = WrapMode.Loop;
animation["jump"].layer = 10;
animation["jump"].wrapMode = WrapMode.ClampForever;
}
Use the start function to set up all of your animations, I set most of my animations on layer ten so that I can put different animations above or below (depending on what I think works)
if ((MovementY > (0.1*(.5*scale))) !(controller.collisionFlags CollisionFlags.Below))
{
animation.CrossFade("jump");
if (playedSound == false)
{
if (jumpSound)
AudioSource.PlayClipAtPoint(jumpSound, transform.position);
playedSound = true;
}
}
else if ((MovementY < (-5*(.5*scale))) !(controller.collisionFlags CollisionFlags.Below))
{
animation.CrossFade("fall");
}
Using something like this will actually determine how you want your animation to work, the ‘playedSound’ boolean can be used as an animation trigger so that it will play the animation once (playedAnimation, or something). If you want your character to stop moving while playing the attack animation, you can set the animation on the same layer so that it over-writes others. Or you can set it on higher/lower layers so that the animation takes precedence:
animation["huff"].layer = 11;
animation["huff"].wrapMode = WrapMode.ClampForever;
animation["huff"].blendMode = AnimationBlendMode.Additive;
animation["blow"].layer = 11;
animation["blow"].wrapMode = WrapMode.Once;
animation["blow"].blendMode = AnimationBlendMode.Blend;
For me, my character breathes in and blows, so what I have is that the animation takes precedence over my other animations. Using the “Once” WrapMode says that it will play and that’s it.
And don’t worry about having it play multiple times, your script should only call upon the animation once:
function Blow()
{
animation.Play("blow");
}
function Huff()
{
animation.Play("huff");
}
function FailBlow()
{
animation.Stop("huff");
}
depending on your attack script.