Aim Animation Script

Hey guys I´m trying to make an FPS. My character has a gun and I made an Aim Animation.
In the Aim Animation the camera just goes from the face of the person to the gun.
So I made a script and attachted it to the camera of my FPS.
Also I putted the Wrap Mode of my Aim-Animation to Once
When I now start my game and try to aim , the Animation begins and ends very fast the hole time.
When I click again the animtion stops.
How can I make that the Animation will hold ,till I click my mouse button again?

Here is the script I´m using :

var PlayAim : boolean = false;

function Update() {

if(Input.GetMouseButtonDown(1))
PlayAim = !PlayAim;

if (PlayAim) {
animation.Play();
}
else {
if (!PlayAim)
animation.Stop();

}
}

Perhaps:

if(Input.GetMouseButtonDown(1)  !PlayAim)
PlayAim = true;
if(Input.GetMouseButtonDown(1)  PlayAim)
PlayAim = false;

Since PlayAim is boolean.

Ok thx , I dont´t know maybe I have a mistake in the script because now the Animation doesn´t work anymore.When I press the mousebutton nothing happens.This is the script now :

var PlayAim : boolean = false;

function Update() {

if(Input.GetMouseButtonDown(1) !PlayAim)
PlayAim = true;

PlayAim = !PlayAim;
if (PlayAim) {

animation.Play(“Aiming”);
}
else {
if(Input.GetMouseButtonDown(1) PlayAim)
PlayAim = false;
if (!PlayAim)

animation.Stop(“Aiming”);

}
}

Try this:

var PlayAim : boolean;

animation.Stop();

function Update() {

if (Input.GetMouseButtonDown(1)  !PlayAim){
PlayAim = true;
}
else if (Input.GetMouseButtonDown(1)  PlayAim){
PlayAim = false;
}
if (PlayAim) {
animation.Play("Aiming");
}
if (!PlayAim){
animation.Stop("Aiming");
}
}

When declaring a boolean it is false by default, so you don’t need the =false part.

Since PlayAim is a boolean it can only be true or false, so PlayAim = !PlayAim won’t work.

Here’s a link to an old learning project of mine that could help you with the animation basics:

LINK

There’s a project attached at the bottom of the second post.

Ok thx I made a second animation. In this animation the player isn´t aiming. So I made a second script and just renamed “aiming” into “normal”. Now when I start my game the player doesn´t do anything" But when I press the mouse button the player aims and when I press it again the play ends aiming.
Thanks for the script now it works:smile: