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 Pavan Shinde · Feb 11, 2013 at 06:08 AM · onmousedrag

OnMouseDrag() not working ...

There are game objects in the scene with a mesh and collider each. Following script is attached to these game objects.

   void OnMouseDrag()
     {
       Debug.Log(" Dragging ...");
     }

It doesn't print the message every time when the game object is dragged. Any help would be appreciated.

Comment
Add comment · Show 5
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 iwaldrop · Feb 11, 2013 at 06:25 AM 0
Share

As long as the object isn't part of the IgnoreRaycast layer, and I'm not running on iPhone, it works every time for me. Are you clicking in and out of the game window? That will cause it, because the first click back into the game window gives the game mouse focus ins$$anonymous$$d of registering a click.

avatar image robertbu · Feb 11, 2013 at 06:33 AM 0
Share

Not sure if this is your problem but: On$$anonymous$$ouseDrag() is only called if an On$$anonymous$$ouseDown() has been called on that object. That is, the first object clicked on is the one that Unity sends the drag messages to (even if it does not have an On$$anonymous$$ouseDrag()). So if you have several objects in the area, your dragging messages will go to the first collider that is found at the click. If you have transparency in your objects, this can be confusing because you cannot click through the transparency (i.e. Unity is using the colliders to figure out what object should be getting the messages).

avatar image OmarAlhaddad · Feb 11, 2013 at 10:35 AM 0
Share

Check how big your colliders are and if they are going through your camera, if they go through the cameras clipping panes it wont work i think.

avatar image Rodolinc · Aug 29, 2014 at 09:51 PM 0
Share

I had the same problem and I just closed Unity and $$anonymous$$onodevelop and restarted and suddenly it worked. Didn't know what caused that.

avatar image adriandevera · Aug 13, 2018 at 01:57 AM 0
Share

Check my solution using the interface draggable script. It works with Gameobjects, sprites and both PC and $$anonymous$$obile Targets. (;

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by adriandevera · Aug 13, 2018 at 01:51 AM

ive created this cheat sheets I use for draggables in all my projects. One for gameobjects and another for UI. Please follow the instructions. It is recommended you try the script in a new scene to make sure the setup is correct before making changes to your current.

Edit: I realized how horrible this looks on here but go ahead and copy and paste it into Unity and follow along from there. (:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 /*
     Instructions:
         1. Create one of the following:  
             a. Cube (this will have a box collider which we'll need for game objects)
             b. Sprite (this will not need a collider, we'll use a different peice of code for UI stuff)
         2. Attach this script onto our objects from above
         3. Go to your Main Camera Game Object in the scene and add a Physics 2D Raycaster
         4. In the "Hierarchy" panel, click "Create", select "UI", and create a new "Canvas".
             Note: This will create a Canvas AND EventSystem (we want both because the Event system also has a Stand Alone Input Module.)
         4. Edit the code depending if you are using a GameObject OR UI object
         5. Now youre done! Click and drag the cube (:
 
     You can add more Interface handelers such as IBeginDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler and more
 */
 
 public class TestDrag : MonoBehaviour, IDragHandler
 {
 
     public void OnDrag(PointerEventData eventData)
     {
         // Pick one peice of code depending on Step 1 in the instructions and comment out the other. In this example we are using a cube so I commented out the sprite example
 
         // For Game Objects: Use below to use this script for Game Objects like our cube because we convert where our mouse points to world coordinates with ScreenPointToRay()
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         transform.position = (new Vector3(ray.origin.x, ray.origin.y, 0));
 
         // For UI Sprites: Uncomment below to use this script for UI objects like sprites, raw images, etc because we use the actual mouse input without converstion needed
         //transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
             // Warning: Adding a Canvas to the sprite object will make the object unmoveable.
     }
 
 }
 
Comment
Add comment · Show 3 · 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 Pavan Shinde · Feb 11, 2013 at 12:44 PM 0
Share

Thanks for your replies. @iwaldrop: i'm working on pc. The problem is i've two cameras, one is main camera and other is used for render texture, when i remove the later camera it works fine, but when it is present onmousedrag() not working all the time.

avatar image Willard720 Pavan Shinde · Aug 12, 2018 at 10:22 PM 0
Share

This is not an answer to the question, this is a comment

avatar image hammad-zahid · Sep 11, 2019 at 06:32 PM 0
Share

great. worked. for angle restricted rotation

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

15 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

Related Questions

How to read Shift + Click input? 1 Answer

Two Strange MathF Clamp Issues: The Disabling of OnMouseDrag 2 Answers

Vector3 to Float? Trying to accomplish a MouseDrag.. 1 Answer

Starting drag onMouseEnter 0 Answers

Switch the camera when object is clicked! 2 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