- Home /
void OnMouseDown for mobile?
hey! i wanted to use this snippet: http://pastebin.com/Mn6sx1MC but somehow it doesent work on a phone - but perfect in a editor. i think that OnMouseDown at line 30 is causing the problem. so this line:
void OnMouseDown(){
beingDragged = true;
}
void OnMouseUp(){
beingDragged = false;
transform.position = oldPosition; // place on the last valid position
_intersecting = 0; // stationary block do not intersect anymore
TintRed(_intersecting); // set the tint one last time
}
how do i have to change this to make this work on a phone?
Answer by incorrect · Apr 20, 2016 at 12:59 PM
OnMouseDown() is an old way of interaction. There is no mouse on mobile platform so interactions are handled in a different way. That's why it works in Editor (on PC with a mouse) and does not on mobile.
In Unity 4.6 we got new UI system as well as new Event System.
So to achieve what you want you should have Event System in your scene that will send interaction events. And your interactable objects should have some way of receiving those events.
For a purposes of interaction with GameObjects you can make them have colliders (2d or 3d) and event handler components: either default EventTrigger or your own script that implement Supported Events handling.
Also you need to add a Raycaster of proper type to your camera that will send those events.
In your case the old OnMouseDown() method will be replaced by new OnPointerDown() wich works on both PC and mobile platforms.
Hope this will help you to get the idea of interaction in Unity and solve your problem. If you have any questions left, feel free to ask.
Answer by mostwanted9192 · Apr 20, 2016 at 09:17 AM
OnMouseDown() is not available on devices because of Touch implementation. For further answer, Kindly look this answer. http://answers.unity3d.com/questions/124198/onmousedown-alternative-for-mobile.html
Answer by aditya · Apr 20, 2016 at 10:32 AM
as Phones and tablets don't have any MOUSES then OnMouseUp or OnMouseDown will not work on them .... try this
void Update(){
if (Input.touchCount == 1) {
if(Input.GetTouch(0).phase == TouchPhase.Began)
beingDragged = true;
if(Input.GetTouch(0).phase == TouchPhase.Ended){
beingDragged = false;
transform.position = oldPosition;
_intersecting = 0;
TintRed (_intersecting);
{
}
}
Answer by Nikunj-Kareliya · Apr 20, 2016 at 09:01 AM
Other alternative is to use methods of Input class inside the Update().
void Update () {
if(Input.GetMouseButtonDown(0)) {
beingDragged = true;
}
else if (Input.GetMouseButtonUp (0)) {
beingDragged = false;
transform.position = oldPosition; // place on the last valid position
_intersecting = 0; // stationary block do not intersect anymore
TintRed(_intersecting); // set the tint one last time
}
}
ok, thanks. that sounds pretty reasonable. but somehow my drag function is acting weird now: http://imgur.com/a/cQGpU
the second gif is how it was before - but not working on mobile. the first how it is after the code changes
// Suppsoe it is perspective camera view
void Update () {
if (Input.Get$$anonymous$$ouseButton(0)) {
// Convert mouse position into world position
// make sure you have passed the distance between object and camera as a 3rd parameter of vector3() method
Vector3 worldPos = mainCamera.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20f));
Vector3 tmpPos = cloneBasket.transform.position;
// Drag only in X axis
tmpPos.x = $$anonymous$$athf.$$anonymous$$oveTowards (tmpPos.x, worldPos.x, Time.deltaTime * 5);
// Restriction of object in X axis
tmpPos.x = $$anonymous$$athf.Clamp (tmpPos.x, 1502.8f, 1507);
cloneBasket.transform.position = tmpPos;
}
}
Answer by meat5000 · Apr 20, 2016 at 10:42 AM
The modern Unity 5 way is to disregard the concept of Mouse and Touch and move to the concept of 'Pointers'. The Standalone Input Module (Touch Input Module now deprecated) has all the methods to handle events from multiple Pointers, which include both cursor and touch input.
http://docs.unity3d.com/530/Documentation/ScriptReference/EventSystems.StandaloneInputModule.html
The Standalone Input Module is designed to work with new UI through the Event System.
To implement Dragging you can use OnDrag
Your answer
Follow this Question
Related Questions
OnDestroy collision detect 4 Answers
Stop a Lerp from looping 3 Answers
OnMouseUp error 0 Answers
iOS multiTouch / release - Strange problem 0 Answers
OnMouseDown If-statement Question 3 Answers