- Home /
The question is answered, right answer was accepted
How can i drag objects smoothly with touch? Please help me with this code!
the object keeps on dropping after sometime and when i drag it fast then it loses hold!
if(t.phase==TouchPhase.Moved)
{
print("Moving touch");
Ray ray=Camera.main.ScreenPointToRay(t.position);
RaycastHit hit=new RaycastHit();
if(Physics.Raycast(ray,out hit, 1000f))
{
if(hit.collider.gameObject==this.gameObject)
{
Vector3 curScreenPoint=new Vector3(t.position.x,t.position.y,Screenspace.z);
Vector3 curPosition=Camera.main.ScreenToWorldPoint(curScreenPoint)+offset*t.deltaTime;
transform.position=curPosition;
}
}
}
It looks like you are checking to see if the object is still under your finger, so if your finger moves past the object, then it is dropped (transform.position=curPosition; is never reached in that code if raycast not on object).
You should consider 3 phases :
when first touched, raycast and store reference to object
while moving, update position
when finger is lifted, remove the reference to the object (drop the object)
Answer by Abhiroop-Tandon · Mar 24, 2016 at 04:03 PM
This might help (helped me anyways) http://answers.unity3d.com/questions/600578/dragging-an-object-by-touch.html
Follow this Question
Related Questions
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers
Smooth Camera/Object Movement 1 Answer
Smooth Grid Movement 0 Answers
Smooth rotation... 1 Answer