- Home /
How can i activate the OnDrag event without dragging my mouse?
I'm implementing an Action Bar system where the user can drag any ability into any other slot. If the user moves the ability to an empty slot, I have that working fine. If the user moves the ability to an occupied slot, right now I have the choice of making it take over the data from the occupied slot or swapping the data. Neither of these solutions seem good enough for me.
I would like for the occupied slot to be picked back up so that I can move it to a new slot. This seems like the most user-friendly solution (and lots of MMOs do this, e.g. WoW).
I'm currently using OnBeginDrag, OnDrag and OnEndDrag functionality to do what I need. Ideally I'd like the occupied ability to be the 'eventData' of 'OnDrag' when i let go of my left mouse button but I can't find a way to do this (aka call the Event while not actually having mouse button held).
Here is what I am doing as it relates to the question. The first line is the only code that is in OnDrag and the other lines are the basics of what I am doing in OnEndDrag.
rectTransform.transform.position = Input.mousePosition;
string tempAbilityName = newAbilitySlot.GetComponent<RunAbilityAndCooldowns>().abilityName;
newAbilitySlot.GetComponent<RunAbilityAndCooldowns>().abilityName =
currentlyDraggedAbility.GetComponent<RunAbilityAndCooldowns>().abilityName;
currentlyDraggedAbility.GetComponent<RunAbilityAndCooldowns>().abilityName = tempAbilityName;
This stores the ability in the occupied slot temporarily, sets the new ability to the old, and the old to the temporary (aka new). I'm doing this for many variables which are stored in each instance of the script on each action bar slot. This allows me to swap abilities easily, but ideally I'd like to be able to pick up the 'newAbilitySlot' instead of just placing it where currentlyDraggedAbility was.
Answer by Casiell · Apr 01, 2020 at 09:17 AM
The idea is to separate your moving methods from OnXDrag events. Drag events should only parse the eventData and call movement methods with parsed parameters. This way you will be able to mock those parameters yourself and achieve what you want.
If you want more specific help, then you need to provide some code.
Sorry, not entirely sure what you mean by this. I added some of the code that I'm using to make the switches during OnDrag and OnEndDrag.
Your answer
![](https://koobas.hobune.stream/wayback/20220612230230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to move an object X or Y axis only 1 Answer
Rotation of gameobject while dragging on the screen not working as expected in unity 4.6 0 Answers
restrict scaling 2 Answers
Adding Accelerating to a Wheel of fortune 0 Answers
"Image.raycastTarget = false" not consistent between PC and Android 1 Answer