- Home /
how to drag an object in 3d world with finger
Hi,
I am working on unity now and I want to drag a cube with my finger on the mobile device like tablet. Now I can drag the cube using raycast but the cube only move in 2d world, only the x, y coordination changes, I want to be able to drag the cube in 3d world, wish all the x, y, z axis can change. Thank you.
if(Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
ray = Camera.main.ScreenPointToRay(touch.position);
LayerMask layerMask = 1 << 9;
if(Physics.Raycast(ray,out hit,100,layerMask)&&Input.GetTouch(0).phase == TouchPhase.Moved)
{
//probe = hit.transform;
hitObjects = hit.transform;
isHit = true;
hit.transform.Translate(Input.GetTouch(0).deltaPosition*Time.deltaTime);
//hit.transform.position = touch.deltaPosition*0.2f;
//hit.transform.position += Input.GetTouch(0).deltaPosition*0.02f;
}
}
Answer by DaveA · Dec 14, 2012 at 08:34 PM
I would find the object's Viewport space coords (Camera.main.WorldToViewport()), move it in viewport coords, then convert that back to world coords (Camera.main.ViewportToScreen()). Keep the same Z value, just alter x,y, so the object moves in the plane that is z units from the camera, no matter which way the camera is looking.
That helped, thank you. I have one more question. I can drag the cube on touch screen with finger but it isn't smooth. $$anonymous$$g., if I move my finger too fast, the cube can't follow my finger. if I move it slowly, the cube move really fast. I want the dragging to be as smooth as possible. whenever i move my finger, the cube will stay with it, same speed, same position. it will be really appreciated if you can provide me some code.
if(Input.touchCount == 1) { Touch touch = Input.GetTouch(0); ray = Camera.main.ScreenPointToRay(touch.position); Layer$$anonymous$$ask layer$$anonymous$$ask = 1 << 9; if(Physics.Raycast(ray,out hit,100,layer$$anonymous$$ask)&&Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
//probe = hit.transform;
hitObjects = hit.transform; isHit = true; hit.transform.Translate(Input.GetTouch(0).deltaPosition*Time.deltaTime); //hit.transform.position = touch.deltaPosition*0.2f; //hit.transform.position += Input.GetTouch(0).deltaPosition*0.02f;
} }
You might want to edit your original question and post/format the code snippet there. $$anonymous$$y guess is you are only hitting/moving when finger is over the object, and when finger is moving, it can get ahead of the object, stopping it. I would detect a 'select' on finger-down, and not check for hit on finger-drag, so you can move your finger anywhere and set the position to that spot.
Right. Yeah I would have a 'selected' boolean which is set if the phase is 'begin' and ray hits it, and set false on phase is 'end', but don't do the raycast if phase is 'move'. And only move if selected is true.
i cast a ray from camera to the cube and drag the cube as finger touches the cube and move it. It should work under the move phase,right?
i don't want the result that wherever i touch the screen and the cube will be there. I really wanna the dragging effects.
Your answer
Follow this Question
Related Questions
Drag Object Via Axis Handles 0 Answers
A node in a childnode? 1 Answer
Curve a Ball in 3d space 4 Answers
Why don't have any dinamic shadows with -force-opengl? 0 Answers
Creating a World? 2 Answers