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 wodawick · Jan 03, 2015 at 04:53 AM · inputloops

Make input code execute once on MouseDown

Hey all,

I have a small problem i need to make the following Code execute once only on MouseDown but have no idea how to do it

             if (Input.GetMouseButtonDown (0)) {
                     TileRay = Camera.main.ScreenPointToRay (Input.mousePosition);
                     if (Physics.Raycast (TileRay, out TileRayHit))
                             ;
                     TileSelected = TileRayHit.transform.name;
                     MouseFirstPressPosition = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                     {
                             Debug.Log (TileSelected);
                             Debug.Log (MouseFirstPressPosition);
                     }
             }
             if (Input.GetMouseButtonUp (0)) {
                     MouseSecondPressPosition = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                     MouseCurrentSwipe = new Vector2 (MouseSecondPressPosition.x - MouseFirstPressPosition.x, MouseSecondPressPosition.y - MouseFirstPressPosition.y);
                     MouseCurrentSwipe.Normalize ();
                     {
                             Debug.Log (MouseSecondPressPosition);
                     }
             }
             if (!(MouseSecondPressPosition == MouseFirstPressPosition)) {
                     if (Mathf.Abs (MouseCurrentSwipe.x) > Mathf.Abs (MouseCurrentSwipe.y)) {
                             if (MouseCurrentSwipe.x < 0) {
                                     Debug.Log ("Swipe Left");
                                     MouseMovementDirection = "Left";
                             } else {
                                     Debug.Log ("Swipe Right");
                                     MouseMovementDirection = "Right";
                             }
                             } else {
                             if (MouseCurrentSwipe.y < 0) {
                                     Debug.Log ("Swipe Down");
                                     MouseMovementDirection = "Down";
                             } else {
                                     Debug.Log ("Swipe Up");
                                     MouseMovementDirection = "Up";
                             }
                     }
             }

The Code works and does exactly what i want but i keeps returning the TileSelected and MouseMovement Direction constantly. not good when you a making a tile game that relies on if statements to move tiles.

Comment
Add comment · Show 2
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 BMayne · Jan 03, 2015 at 06:50 AM 0
Share

Is this inside of OnGUI function?

avatar image wodawick · Jan 04, 2015 at 02:55 AM 0
Share

No it wasn't inside a OnGUI function. I managed to figure it out Thanks to HYOERSAVV. Using a If statement for mouse button up made it work perfectly. Thank you.

1 Reply

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

Answer by HYPERSAVV · Jan 03, 2015 at 05:20 AM

If you are not clearing out the values of MouseFirst/SecondPressPosition, then there will always be two values to compare and therefor you will most likely always hit that third if statement. Perhaps you could roll that third if statement into the if statement for GetMouseButtonUp, I'm assuming you only want to do this after the mouse was released.

Comment
Add comment · 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

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

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

multiple inputs 2 Answers

Code behaves unexpectedly when not in FixedUpdate 1 Answer

Putting variable into new Unity GUI InputField and then Acessing that variable 1 Answer

Scripting a Konami Code 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