- Home /
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?
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");
}
}
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
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.
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...
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...
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.