Question by
genericname24 · Feb 22, 2018 at 09:25 PM ·
c#mobileaxistouch controlsdrag-and-drop
How do you move 3D object by touch on mobile
At the moment my camera has a rotation of 52 on the x axis and zero on all others. The touch code i have been using primarily moves the object on the y axis which would be fine for 2D but i want to move it on the x and z rather than the x y. i have tried making changed to the current code but when i switch axis the object disappears instead of now moving along the z as currently moving on the y causes the object to fall through the floor when i drag it to certain spots. I am probably missing something but admittedly im not too familiar with mobile development.
The code using(Actually slightly tweaked from a answer from a question from someone else)
Vector3 v3;
if (Input.touchCount != 1) {
shouldDrag = false;
return;
}
Touch touch = Input.touches[0];
Vector3 pos = touch.position;
if(touch.phase == TouchPhase.Began) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(pos);
if(Physics.Raycast(ray, out hit) && (hit.collider.tag == "Penguin"))
{
Debug.Log ("Here");
selection = hit.transform;
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = new Vector3(pos.x, pos.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = selection .position - v3;
shouldDrag = true;
}
}
if (shouldDrag && touch.phase == TouchPhase.Moved) {
v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
selection .position = v3 + offset;
}
if (shouldDrag && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) {
shouldDrag = false;
}
Comment