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 sdgd · Jan 31, 2013 at 11:20 AM · c#mouse-drag

how to right click OnMouseDrag C#

I want with right mouse button to drag how would I do that?

 void OnMouseDrag (){
     if (Input.GetMouseButtonDown(1)){Debug.Log("asdeglkzug");}
 }

I know it only allows me with left button but why and how do I fix 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

2 Replies

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

Answer by $$anonymous$$ · Jan 31, 2013 at 01:29 PM

You could write a script that can handle dragging for all mouse buttons, but I'm in a good mood and I wrote a basic one. The basic idea is checking if a button is down, and if it is down, check if it was down in the previous frame too. If yes, then we are dragging. During these checks, you can add your own mechanism for example if you want a drag to happen on a collider or gui element, these are all checks you can even implement if you temper directly with this class or even better, subclass this and write further checks in the callbacks.

To make it easier to extend, I separated the drag callbacks to start drag, dragging, end drag, so one can flexibly extend this script with subclassing, since the basic fact that the user is dragging with a mouse button is independent of any further logic.

This has another neat feature, that is you can drag with multiple buttons at once, callbacks will be called for each mouse button. The script:

 public class MouseDrag : MonoBehaviour {
 
     // array for storing if the 3 mouse buttons are dragging
     bool[] isDragActive;
 
     // for remembering if a button was down in previous frame
     bool[] downInPreviousFrame;
 
     void Start () {
         isDragActive = new bool[] {false, false, false};
         downInPreviousFrame = new bool[] {false, false, false};
     }
     
     void Update () {
         for (int i=0; i<isDragActive.Length; i++)
         {
             if (Input.GetMouseButton(i))
             {
                 if (downInPreviousFrame[i])
                 {
                     if (isDragActive[i])
                     {
                         OnDragging(i);
                     }
                     else
                     {
                         isDragActive[i] = true;
                         OnDraggingStart(i);
                     }
                 }
                 downInPreviousFrame[i] = true;
             }
             else
             {
                 if (isDragActive[i])
                 {
                     isDragActive[i] = false;
                     OnDraggingEnd(i);
                 }
                 downInPreviousFrame[i] = false;
             }
         }
     }
 
     public virtual void OnDraggingStart(int mouseButton)
     {
         // implement this for start of dragging
         Debug.Log("MouseButton" + mouseButton + " START Drag");
     }
 
     public virtual void OnDragging(int mouseButton)
     {
         // implement this for dragging
         Debug.Log("MouseButton" + mouseButton + "DRAGGING");
     }
 
     public virtual void OnDraggingEnd(int mouseButton)
     {
         // implement this for end of dragging
         Debug.Log("MouseButton" + mouseButton + " END Drag");
     }
 }
Comment
Add comment · Show 4 · 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 sdgd · Feb 01, 2013 at 12:03 AM 0
Share

oh umh thought there's better solution than workarounds

well I think I'm better off with creating

On$$anonymous$$ouseOver

and inside on mouse down the correct key it's less calling it

but hey thanks that code might be handy for someone else

I just hope it didn't tired you too much

avatar image $$anonymous$$ · Feb 01, 2013 at 01:19 AM 0
Share

no problem, I think a "workaround" is not a bad thing in general.. I mean if an API offers a function only partially, you need to do something about it.. if you subclass this script, you can do all kinds of things with drag, and don't have to rely on the built-in callback which only registers if you drag on a collider or gui element.. you could also "just drag", like in a RTS game, which could be pretty "workaround-ish" if you wanted to go with the built-in On$$anonymous$$ouseDrag. Anyway, I hope this will help someone.

avatar image knowledgelens · Jun 22, 2016 at 08:05 AM 0
Share

Hey, it's a very useful one...

But when I use this, every objects having this script are responding, not only the object I have just clicked. can u help me on this.

sorry for my poor English...

avatar image knowledgelens · Jun 22, 2016 at 03:58 PM 0
Share

Hey, it's a very useful one...

But when I use this, every objects having this script are responding, not only the object I have just clicked. can u help me on this.

sorry for my poor English...

avatar image
1

Answer by JohntyGTN · Jan 31, 2013 at 01:43 PM

I think you have to write the function yourself. As far as I know those functions only work with left click. To simulate you would have to detect the right click in Update, see if there is a collision with the mouse and object you are targeting then see if the mouse has moved.

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

11 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

Related Questions

Trajectory on mouse up 0 Answers

How to click and drag an object at full speed 2 Answers

Moving game objects from the second camera 0 Answers

Drag a Specific GameObject around the screen in a 2.5 sidescroller 2 Answers

Moving Rigidbody2D relative to the mouse. 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