- Home /
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.
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.
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).
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.
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.
Check my solution using the interface draggable script. It works with Gameobjects, sprites and both PC and $$anonymous$$obile Targets. (;
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.
}
}
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.
This is not an answer to the question, this is a comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613092508im_/https://answers.unity.com/themes/thub/images/avi.jpg)