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?
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.
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
}
}
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.
The Standalone Input Module is designed to work with new UI through the Event System.