- Home /
How to disable animation.Play? (FPS)
Well, i'm making and FPS, and i'm having trouble with the Aim Down Sights thing, well, i'm my gun script, i have this lines, that play animation and sound when i shoot (press Fire1):
if(Input.GetButton("Fire1")){
if(ammo > 0){
if(shootTime > 0.1) {
ammo--;
audio.clip = shootSound;
audio.Play();
animation.Play("FireAnim1"); Instantiate(bullet,gunBarrel.position,gunBarrel.rotation);
shootTime = 0;
(All variables are alright).
But them, i made an Aim Down Sights system based on animations, so I added this lines:
if(Input.GetButtonDown("Fire2")){
animation.Play("ADSAnim1");
}
if(Input.GetButtonUp("Fire2")){
animation.Play("ADSAnim2");
}
But then, while I hold "Fire2" the gun stays in ADS position (ok till now) but when i shoot (press Fire1) qhile holding "Fire2", the gun goes to the non-ads position, plays the normal shooting animation, and then stays there, till I stop holding "Fire2" (then it plays "ADSAnim2", wich is the reverse of the "ADSAnim1", in other words, the gun goes from the ADS position to the normal position).
Well i tried adding some lines like if(Input.GetButtonDown("Fire2")); //Don't play animation or something like this but i'm not skilled in JavaScript so idk how to do that.
Please help me and sorry for my english, hope you understood all :D
Answer by b1gry4n · Nov 30, 2014 at 04:42 PM
There are many ways to write the following code. I will try to write it in an "easy to understand" way.
Input.GetButton : Called every frame the player is holding this button down.
Input.GetButtonDown : Called for 1 frame when the player presses this button. It will not retrigger again until the player has released the button and pressed it again.
Input.GetButtonUp : Called for 1 frame when the button is lifted.
An easy way to tell if the player is aiming down the sight or not is to create a boolean.
if the player is aiming down the sights: aimingDownSights = true;
if the player is not aiming down the sights: aimingDownSights = false;
var aimingDownSights: boolean = false;
If you truly want the player to have to hold down the right mouse button while aiming down the sights (and not just a toggle) you can use:
if(Input.GetButton("Fire2"){
aimingDownSights = true;
//turn on aiming down sight anim
}else{
if(aimingDownSights){
aimingDownSights = false;
//turn off aiming down sight anim
}
}
I am assuming you already have 2 separate animations for hip fire and aiming down the sight.
if(Input.GetButton("Fire1"){
if(aimingDownSights){
// aim down sight fire
}else{
//hip fire
}
}
If you do not want to allow automatic firing (the player must click for every shot) use this:
if(Input.GetButtonDown("Fire1"){
if(aimingDownSights){
// aim down sight fire
}else{
//hip fire
}
}
Thanks, it worked, had some problems to understand but it worked, thank you :D
Your answer
Follow this Question
Related Questions
An instance of type (Script) is required to access non static member (Script) 1 Answer
I can't open boo or js scripts in Monodevelop with a default install? 3 Answers
Lens-Flare Muzzleflash 0 Answers
Enemy death help 1 Answer
(JS) Best way to make it so that a player can only issue a "move click" once? 1 Answer