Simple Shoot Animation

Ok, here am I, (I’m currently trying to learn how to program unity so I’m VERY not good at this)
The only thing I want is:

  • When I touch nothing, the animation ‘‘Idle’’ plays
  • If i press ‘‘E’’ on my keyboard, stop the ‘‘Idle’’ and play once ‘‘Shoot’’
  • When ‘‘Shoot’’ is finished, go back to ‘‘Idle’’
  • If I press ‘‘E’’ when ‘‘Shoot’’ is playing, just redo the ‘‘Shoot’’ animation.
  • If I press ‘‘E’’, the sound ‘‘Fire1’’ will play once

If someone has some time to help me, I will very appreciate it. (And if you can send me a script and explain it, I will VERY apreciate it :smile:)

var BulletCount : int; //Ammount of bullets remaining in the clip
var MagCapacity = 10; //The maximum capacity of bullets in the clip
var MuzzleFlash : GameObject; //The muzzleFlash prefab
var MuzzleFlashSpawn : GameObject; //The muzzleFlash spawn point object
var BulletSpawn : GameObject; //The ejected bullet spawn point object
var Bullet : GameObject; //The bullet prefab that gets ejected
var bulletSound : AudioClip;
var compensationTime = .1;

function Start()
{
	animation["fire"].speed = 3;
	animation["fire"].wrapMode = WrapMode.Clamp; 
        animation["idle"].wrapMode = WrapMode.Loop; 
	animation.Play ("idle", PlayMode.StopAll);	//start by playing the idle animation in loop
}

function Update () 
{
	if (Input.GetKey ("E")  animation.IsPlaying("idle")  BulletCount >= 1)  //check to see if ready to fire
	{
	animation.wrapMode = WrapMode.Clamp; //using clamp allows the detection of the end of the animation to be more accurate
	animation.Play ("fire"); //play the firing animation
        SendMessage("FireBullet");
        SendMessage("SoundEffect", bulletSound);
	}
	else if (Input.GetKey ("E")  animation.IsPlaying("idle")  BulletCount <= 0) //if the bullets left in the clip is 0
	{
	SendMessage("Reload");
	}
        else	{
        animation.Play("idle");
        }	
}

function Reload ()

{
	yield WaitForSeconds(compensationTime); 
        BulletCount = MagCapacity;	//set the bullet count to = the maximum clip size
}

function FireBullet () //Called via animation event to make the muzzle flash

{
MuzzleFlashSpawn.transform.Rotate(Vector3.up * Random.Range(0,360)); //set the spawn point to have a random rotation vector
Instantiate(MuzzleFlash, MuzzleFlashSpawn.transform.position, MuzzleFlashSpawn.transform.rotation); //instantiate the muzzle flash
}

function SoundEffect (clip : AudioClip) //Called via animation event to make a sound effect

{
	var randomPitch = (Random.Range(1.0,2.0));
	audio.pitch = randomPitch;
	audio.PlayOneShot(clip);
}

Hi
You can use Character animation on Unity’s web site as a tutorial
it will definitely help you

wow quick awnser, I will try the code right now

sorry, that was set for “Fire1” (AKA left mouse button), so i updated it to “E”

JM, your animation script is pretty solid, except for the way your changing the WrapMode in Update(); you’re changing the WrapMode of ALL of this object’s animations every frame, just because you want to play one animation with that WrapMode, which is a massive waste of CPU cycles.

You could set the WrapMode of specific animations in Start(), like this:

animation["animName"].wrapMode = WrapMode.Clamp;

That way you don’t have to keep flip-flopping WrapModes every frame in Update()

ok

i updated it to that

It looks like you set the WrapMode of those individual animations to “loop” in Start(), but you’re still changing the WrapMode of all animations to clamp right before you play the animations you want Clamped in Update(), even though those are the only animations you want to clamp.

I think it would be something more like this:

function Start()
{
	animation["fire"].speed = 1;

        animation.wrapMode = WrapMode.Loop; //set all animations to loop

        //except fire and reload, set them to clamp
	animation["fire"].wrapMode = WrapMode.Clamp; 
        animation["reload"].wrapMode = WrapMode.Clamp; 

	animation.Play ("idle", PlayMode.StopAll);	//start by playing the idle animation in loop
}

function Update () 
{
	if (Input.GetKey ("Fe")  animation.IsPlaying("idle")  BulletCount >= 1)  //check to see if ready to fire
	{
	    animation.Play ("fire"); //play the firing animation
	}
	else if (Input.GetKey ("E")  animation.IsPlaying("idle")  BulletCount <= 0) //if the bullets left in the clip is 0
	{
	    animation.Play ("reload"); //reload the gun
	}		
}

I updated it again! there!!!

ok but where do i put my animation? Do I just let them in the projet bar? Right now, I have ‘‘fire’’ and ‘‘idle’’

EDIT: ok nvm. But when i shoot, after the ‘‘fire’’ animation, there is nothing. I mean the gun don’t do the ‘‘idle’’ animation

I updated it again, it should work now. that was for something else, and it was still in the same form as it was originally.

lol ok then

Edit: ok now, the problem is when i shoot, the animation fire don’t have tim to finish before idle returns. Thx for you effort :smile:

what? maybe try it now? what do you mean by that?

well my fire animation dont have time to finish.
-When i press E, I just see the first frame of the fire animation and then idle returns…

well, try it now, i updated it, again, so the animation plays faster, and now there is a short delay

Thx trying now :smile:

Edit: nope. Sorry. Not fixed.

can you give me the gun so i can test it to get it to work?

How am I suposed to do that??? (Remember, I’m a noob)
Do i send you the unity file? The Gun model?

you reply, but at the bottom, click “Go Advanced” and then click attach file and then upload the gun model, and then it should work