Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 uanmanarmy · Mar 18, 2015 at 10:24 PM · getmousebuttondownmousedragmousedowngetmousebutton

How to make an object appear only when I click and not when I drag

How to make an object appear only when I click and not when I drag

 void Start () 
     {
         col = GetComponent<Collider>();
         handler = new newClass();
         visible = false;
         newPosition = canv.transform.position;
  
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0) ) 
         { 
             isDown = true;
             handler.ray = handler.mainCamera.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (col.Raycast(handler.ray, out hit, 100.0f))
             {
                 //  handler.Delete(gameObject);
                 canv.transform.position = new Vector3(transform.position.x, transform.position.y + 2f, transform.position.z);
 
 
             }
           
       }
         if (Input.GetMouseButton(0))
             canv.transform.position = newPosition;
     }

The problem is that on I get MouseButtonDown the MouseDrag(Mousebutton) is also registered, How I can make to not register it, so when I drag it will go with mousebuttondrag, and when I click it will go only with mousebuttonDown

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

2 Replies

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

Answer by Utamaru · Mar 20, 2015 at 09:26 AM

I would recommend you to use new unity EventSystems. For example, if you want to catch clicks on your gameObjects, just implement IPointerClickHandler interface in script, attached to your gameObject. And then, when click on your object happens, OnPointerClick message will be called. You also need to add PhysicsRaycaster component to your main camera. Don't forget to do that.

Example code:

  public class FindableObject : MonoBehaviour, IPointerClickHandler
  {
      public void OnPointerClick (PointerEventData eventData)
      {
          Debug.Log ("OnPointerClick " + gameObject.name);
      }
  }

This event will not be called if you drag element. For drag event you can implement another interface. Just check EventSystems' interfaces in documentation

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 uanmanarmy · Mar 20, 2015 at 10:16 AM 0
Share

I am going to try it, didn't knew it exists :D

avatar image
0

Answer by MiniFish · Mar 19, 2015 at 07:18 AM

this script looked like you are trying to drag and place something. have you looked into using Input.GetMouseButtonUp()?

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 uanmanarmy · Mar 19, 2015 at 02:37 PM 0
Share

ButtonUp will give an event when releasing the button, but I need when I click it.

avatar image MiniFish · Mar 20, 2015 at 09:22 AM 0
Share

hmm, what about recording the initial input position during buttonDown and then compare the input position in getButton to deter$$anonymous$$e if the mouse did move? if input did move, then you do whatever else you wanted to do, otherwise just ignore.

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

22 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 avatar image

Related Questions

Mouse drag with specific angle 1 Answer

Raycast repeats multiple times 2 Answers

Collider and Distance Problem OnMouseDown 1 Answer

Input.GetMouseButtonUp not detected after drag 1 Answer

How do I separate OnMouseDown() from OnMouseDrag() 0 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