I am trying to drag my game object using iphone touch. I am having the problem that my object is dragging wherever on the screen I touch, I want it to drag only when placing my finger on object. Here is my code:
if (Input.touchCount > 0) {
Touch theTouch = Input.GetTouch (0);
Ray ray = Camera.main.ScreenPointToRay(theTouch.position);
if(Physics.Raycast(targetItem.transform.position, ray.direction,1.5f))
{
if(Input.touchCount == 1) {
float scrollDeltaX = theTouch.deltaPosition.x;
float scrollDeltaY = theTouch.deltaPosition.y;
scrollDistanceX = Mathf.Clamp(scrollDistanceX+scrollDeltaX*Time.deltaTime,-18,15); //limits of my screen
scrollDistanceY = Mathf.Clamp(scrollDistanceY+scrollDeltaY*Time.deltaTime,-10,12.5f);
targetItem.transform.position = new Vector3(scrollDistanceX,scrollDistanceY,targetItem.transform.position.z);
}
}
}