Third Person Attack Problem

Hi

i am currently working on making a third person fighting game with some friends. we have modeled and animated our character and have already applied a walk script to it. the problem is we cannot figure out how to make the character play his attack animation when the “g” key is pressed. any ideas on what the script should look like? all help is appreciated.

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.

Or you could just use (Input.GetKeyDown(“g”)). No need to go an assign it an atribute or whatever.

I find it easier to set it to an input name such as “attack” so that if my player wants to change the variable, they can ^^