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 /
This question was closed Oct 09, 2013 at 11:29 PM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by gilsdank · Sep 23, 2013 at 02:19 AM · inputgetbuttondown

How can i make GetButton only fire once?

enter code hereOkay so basically, i have 2 objects, both with cameras, and right now im using Input.GetButton("Fire1") to check to see if one of the objects was clicked. It works but when im pressing mouse button its reading getbutton like 4 or 5 times resulting in weird behavior. so my question is, how an i make it so that this betbutton call will only return once per click? GetButtonDown("Fire1") does not work.

 using UnityEngine;
 using System.Collections;
 
 public class CameraControl : MonoBehaviour {
     
     
     public Transform target;
     public Vector3 position;
     public float rotateSpeed = .01f;    
     public float distance = 2.8f;
     public float xSpeed = 250.0f;
     public float ySpeed = 120.0f;
     public float yMinLimit = -80;
     public float yMaxLimit = 80;
     public float x = 0.0f;
     public float y = 0.0f;
     public Vector3 distanceVector;
     
     
     public Camera curCam;
     public Camera tarCam;
     
     
     
     void CameraSelect() {
         //Debug.Log("1");
         curCam = Camera.current;
         //Debug.Log("2");
         if(Input.GetButton("Fire1")){
             Debug.Log("3");
             RaycastHit rayHit;
             Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition);
             Debug.Log("4");
             if (Physics.Raycast(ray, out rayHit, 1000)){
                 Debug.Log("5");
                 curCam.enabled = false;            
                 tarCam = rayHit.transform.gameObject.GetComponentInChildren< Camera >();
                 tarCam.enabled = true;
             }
         }
     }
     
     // Use this for initialization
     void Start () {
         
         x = transform.eulerAngles.y;
         y = transform.eulerAngles.x;
         distanceVector = new Vector3(0,0,-distance);
         
         if (rigidbody)
         rigidbody.freezeRotation = true;
         
     }
     
     // Update is called once per frame
     void Update () {
         CameraSelect();
         if(target){
             if(Input.GetMouseButton(1)){
                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
          
                  y = ClampAngle(y, yMinLimit, yMaxLimit);
                 
                 Quaternion rotation = Quaternion.Euler(y, x, 0);
                 Vector3 position = (target.transform.position + (rotation * distanceVector));
         
         
                 transform.rotation = rotation;
                 transform.position = position;
                 }
         }
             
             
     }
     static float ClampAngle (float angle, float min, float max) {
     

         if (angle < -360)
             angle += 360;
         if (angle > 360)
         angle -= 360;
         return Mathf.Clamp (angle, min, max);
     }
 }
Comment
Add comment · Show 20
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 syclamoth · Sep 23, 2013 at 02:19 AM 3
Share

GetButtonDown is what you should be doing here. Can you elaborate on why that isn't working for you?

avatar image syclamoth · Sep 23, 2013 at 02:28 AM 3
Share

Use GetButtonDown, but put in Debug.Log commands on every second line. Find out what is getting called and what isn't. From what I can see, I see no reason why GetButtonDown isn't working- presumably there's something unusual happening here.

avatar image syclamoth · Sep 23, 2013 at 02:51 AM 2
Share

Well now that's just strange. Are you getting to the line before the if statement?

avatar image syclamoth · Sep 23, 2013 at 02:57 AM 2
Share

Try using 'Get$$anonymous$$eyDown' ins$$anonymous$$d. Are you getting any errors in the console? Also remember that you will get a lot more 1s and 2s than 3s.

avatar image syclamoth · Sep 23, 2013 at 05:03 AM 3
Share

@gilsdank - I fixed up the formatting for you, but in future remember that you can't just copy-paste code directly into questions! The easiest way to fix it up is to select your code segment and press the '101010' button above the dialog box. Also, you can always edit your own posts to repair formatting etc.- you can do more than just apologise!

Alternatively, you can use xml formatting flags 'code' and '/code' at the beginning and end of your code segment (remember that these have to be encased in angle brackets ), or just add four spaces to the beginning of each line.

Show more comments

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by clunk47 · Sep 23, 2013 at 05:17 AM

Found a few issues. You were defining a few things within functions instead of at the top of your script, some one of them twice (position), and you were defining curCam but not using it. Try this.

 using UnityEngine;
 using System.Collections;
 
 public class CameraControl : MonoBehaviour 
 {
     public Transform target;
     public Vector3 position;
     public float rotateSpeed = .01f;
     public float distance = 2.8f;
     public float xSpeed = 250.0f;
     public float ySpeed = 120.0f;
     public float yMinLimit = -80;
     public float yMaxLimit = 80;
     public float x = 0.0f;
     public float y = 0.0f;
     public Vector3 distanceVector;
     public Camera curCam;
     public Camera tarCam;
     RaycastHit rayHit;
     Ray ray;
     Quaternion rotation;
     
     void Update () 
     {
         CameraSelect();
         
         if(curCam != Camera.current)
             curCam = Camera.current;
         
         if(target)
         {
             if(Input.GetMouseButtonDown(1))
             {
                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
                 y = ClampAngle(y, yMinLimit, yMaxLimit);
                 
                 rotation = Quaternion.Euler(y, x, 0);
                 position = (target.transform.position + (rotation * distanceVector));
                 transform.rotation = rotation;
                 transform.position = position;
             }
         }
     }
     
     
     void CameraSelect() 
     {
         if(Input.GetButtonDown("Fire1"))
         {
             Debug.Log("3");
             
             if(curCam != null)
                 ray = curCam.ScreenPointToRay(Input.mousePosition);
             
             Debug.Log("4");
             
             if (Physics.Raycast(ray, out rayHit, 1000))
             {
                 Debug.Log("5");
                 curCam.enabled = false;
                 tarCam = rayHit.transform.gameObject.GetComponentInChildren< Camera >();
                 tarCam.enabled = true;
             }
         }
     }
     
     // Use this for initialization
     void Start () 
     {
         x = transform.eulerAngles.y;
         y = transform.eulerAngles.x;
         distanceVector = new Vector3(0,0,-distance);
         
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
     
     // Update is called once per frame
     
         
     static float ClampAngle (float angle, float min, float max) 
     {
         if (angle < -360)
             angle += 360;
         if (angle > 360)
             angle -= 360;
         return Mathf.Clamp (angle, min, max);
     }
 }
Comment
Add comment · Show 7 · 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 gilsdank · Sep 23, 2013 at 05:22 AM 1
Share

this works well with no errors, thank you! however the reason i was using Fire1 ins$$anonymous$$d of get mouse button was because i wanted the controls to work across mulitple platforms, but for now i suppose that wont matter

avatar image clunk47 · Sep 23, 2013 at 05:23 AM 2
Share

Just be sure to use GetButtonDown("Fire1") to register a single click, glad I was able to help along with @syclamoth :)

avatar image clunk47 · Sep 23, 2013 at 05:25 AM 2
Share

Also keep in $$anonymous$$d you only need your variables to be set public if you either will change values in the inspector, or they will be accessed from other scripts using GetComponent, unless of course they're static, but I'll stop there for now lol.

avatar image gilsdank · Sep 23, 2013 at 05:26 AM 1
Share

oh no, you did it with getmousebuttondown... dude you rock! haha thanks again

avatar image clunk47 · Sep 23, 2013 at 05:29 AM 3
Share

Be sure to up-vote any answers on UA that help, as well as any comments that helped as well. @syclamoth definitely deserves some karma for all the time he put into this :)

Show more comments

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Double tap mechanics and axis inputs 3 Answers

Input.GetKeyDown doesn't work first button press 1 Answer

Having GetButton act like GetKeyDown 1 Answer

Detecting if 2 Buttons are pressed at the same time 3 Answers

Input.GetButtonDown Unknown member 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