I want to implement a feature in my mobile game where if you press on a game object for a certain amount of time while not moving your finger an animation will play. I also need to be able to drag and drop objects. I have the drag and drop functionality working already, but know I also need the press and hold feature. I have this code at the moment:
private void Update()
{
foreach (var touch in Touch.activeTouches)
{
// Only respond to first finger
if (touch.finger.index == 0 && touch.isInProgress)
{
Ray ray = mainCamera.ScreenPointToRay(touch.screenPosition);
RaycastHit2D hit = Physics2D.GetRayIntersection(ray);
if (hit.collider != null)
{
print("Selected: " + hit.collider.name);
// Drag game object
}
}
}
}
This code works just like I want it. You put a finger on the screen and a ray is used to select an object that I can drag. But I am not sure where to start on the other feature described in the paragraph above, there is a startTime and a startScreenPosition property that maybe can be used for this, but I don't know how. Any help is greatly appreciated.
Thanks.