- Home /
Drag works with mouse but not with touch
I implemented a code to drag a button along with the mouse. When I try to change it in order to make it work in my mobile phone (with Touch), it won't work properly: it is slow, it stops dragging randomly... I've made a lot of attempts and still can't find a solution.
This is my code for mouse click:
public class Arrossegar : MonoBehaviour {
Rect RectBut = new Rect(Screen.width/3f, Screen.height/2f, Screen.width*0.5f, Screen.height*0.2f); bool PressedButton = false;
void OnGUI() {
if (RectBut.Contains (Event.current.mousePosition)){
if (Event.current.type == EventType.MouseDown){
PressedButton = true;
}
if (Event.current.type == EventType.MouseUp){
PressedButton = false;
}
}
if (PressedButton == true && Event.current.type == EventType.MouseDrag){
RectBut.x += Event.current.delta.x;
RectBut.y += Event.current.delta.y;
}
GUI.Button(RectBut, "Draggable button");
}
Does anybody know how can I "translate" this code to make it work with Touches properly? Take into account that I'm new to Unity.
Thank you!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613173521im_/https://answers.unity.com/themes/thub/images/avi.jpg)