Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CJGames · Nov 30, 2014 at 04:32 PM · javascriptfpsadsfirst person shooter

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
    }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CJGames · Nov 30, 2014 at 07:40 PM 0
Share

Thanks, it worked, had some problems to understand but it worked, thank you :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges