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 adrennalinpro · Apr 13, 2014 at 09:57 PM · toggleaim

Aim only works while button is held down... how do I make it a toggle?

So I've got this script that I wrote a while back, and I had set it up to where the mouse button or the alt pos. key had to be held down in order for it to work. The script checks to see if the right mouse button (Fire2) is being held down, and if so it activates the Aim camera. If the mouse button (Fire2) is lifted, or GetButtonUp("Fire2"), then it deactivates the aim camera. I would like to alter this script by making it a toggle, rather than having to hold down the aim button. I've tried changing GetButtonUp to be GetButtonDown, so that it would need 2 clicks in order for it to activate/deactivate, but the script breaks every time I do that. Here's the working script:

     var Aim : boolean = false;
     var Cam : GameObject;
     
     function Update () { 
     
     if(Input.GetMouseButtonDown(1)||Input.GetButtonDown("Fire2")) { 
     Aim = true; 
     
     if(Aim == true) { 
     
     Cam.active = true;
          } 
      }
     
     if(Input.GetMouseButtonUp(1)||Input.GetButtonUp("Fire2")) { 
     Aim = true; 
     if(Aim) { 
     Cam.active = false; 
             }
         }
     }



So any ideas as to how I can get this to work? BTW the only reason I have the || statements that say this button or that button, is because I couldn't get Fire2 to work at some point for some reason, so I just used MouseButton in combination with GetButton, just to be safe. Thanks!

EDIT: I've also tried adding a variable IsAiming, or just using the already available boolean variable Aim in order to see if the gun is already aiming, and if so then if the button is clicked then deactivate. I just can't seem to be able to implement it!

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 robertbu · Apr 13, 2014 at 10:03 PM

Try this:

 #pragma strict
 
 var Aim : boolean = false;
 var Cam : GameObject;
 
 function Update () { 
 
     if(Input.GetMouseButtonDown(1)||Input.GetButtonDown("Fire2")) { 
         Aim = !Aim;
         Cam.SetActive(Aim); 
     } 
 }

Note this code is based on your current code that activates/deactivates the game object. GameObject.active is depriciated, so I'm using SetActive(). You may want to enable/disable the aiming script instead.

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 adrennalinpro · Apr 14, 2014 at 04:43 AM 0
Share

Thanks so much! Totally worked. $$anonymous$$y hero! Lol really, thanks. I've been trying to make it work for awhile. :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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3rd Person Gun Aim 1 Answer

Aiming-recoil animation problem 0 Answers

Get position of mouse aim to world? 1 Answer

I can't aim up and down in my FPS game I've tried to use scripts of the internet but its not working. 1 Answer

Change something in a script with other script 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