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
1
Question by valdiazdanovic · Apr 01, 2015 at 04:42 PM · mouse click

How to different hold and one click left button mouse?

void Update ()

{

  if (Input.GetMouseButtonDown(0))


  {


       // how to code if I press long/hold left button mouse?


       // how to code if I one click left button mouse?


  }


  else


  {


       // this is when I not press left button mouse


  }


}

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
2

Answer by KdRWaylander · Apr 02, 2015 at 07:28 AM

Hi, here you go, it's working (at least the JS, is just translated for the C#, i dind't tested it). You can adjust what you call a "long click" by changing the 0.4 value to whatever you want :)

JS:

 private var t0:float;
 private var longClick:boolean;
 private var shortClick:boolean;
 
 /* INITIALIZATION */
 function Start () {
     t0 = 0;
     longClick = false;
     shortClick = false;
 }
 
 /* READ AT EACH FRAME */
 function Update () {
     if (Input.GetMouseButtonDown(0)){
            t0 = Time.time ;
     }
  
      if (Input.GetMouseButtonUp(0) && (Time.time - t0) > 0.4){
         longClick = true;
     }else if (Input.GetMouseButtonUp(0) && (Time.time - t0) < 0.4){
         shortClick = true;
     }
     
     // Don't forget to reset to false shortClick or longClick as soon as you use it
     // Unless what it will be still true at the next frame and cause issues
 }shortClick = false;
 }


C#:

 float t0;
 bool longClick;
 bool shortClick;
 
 /* INITIALIZATION */
 void Start () {
     t0 = 0f;
     longClick = false;
     shortClick = false;
 }
 
 /* READ AT EACH FRAME */
 void Update () {
     if (Input.GetMouseButtonDown(0))
         t0 = Time.time ;
  
      if (Input.GetMouseButtonUp(0) && (Time.time - t0) > 0.4f){
         longClick = true;
     }else if (Input.GetMouseButtonUp(0) && (Time.time - t0) < 0.4f){
         shortClick = true;
     }
     
     // Don't forget to reset to false shortClick or longClick as soon as you use it
     // Unless what it will be still true at the next frame and cause issues
 }
Comment
Add comment · Show 2 · 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 UtopianKing · Mar 09, 2018 at 11:15 PM 1
Share

In the C# version longClick and shortClick are both defined as floats, though in the code they are assigned with boolean values.

But other than that the code works, and I thank you for it.

avatar image KdRWaylander UtopianKing · Mar 12, 2018 at 03:37 PM 1
Share

Woops, you're right ! I edited that mistake. Thanks !

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

How to make a mouse click selection for in game use 1 Answer

How to do a Burst Particle effect on Mouse Click? 2 Answers

How to disable Touch to Mouse Click translation 2 Answers

Clicking one prefab effects them all. 2 Answers

how to detect mouse click on a gameobject. 7 Answers


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