- Home /
How to move cubes with finger
I'm trying to figure out a way to move cubes with my hand sort of like I select a cube, drag my finger across the screen with the cube still at then edge of my finder and when I lift off the cube stays in the screen position its been moved to.
I cant seem to figure out how to implement this?
I tried coding it myself with:
var target1: Transform; var target2: Transform; var speed:float = 0.1;
function Update () { if (Input.GetMouseButton(0)) { var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if (hit.transform == target1) {
print("Hit target 1");
// Get movement of the finger since last frame
var touchDeltaPosition:Vector2 = iPhoneInput.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate (touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
} else if (hit.transform == target2) {
print("Hit target 2");
}
} else {
print("Hit nothing");
}
}
}
But that didnt seem to work at all.
Any thoughts on this?
Answer by Tetrad · Jun 25, 2010 at 12:56 AM
Your code seems almost identical to a question I answered earlier.
So here: http://answers.unity3d.com/questions/12872/using-touch-doesnt-get-the-correct-vector/12874#12874
Answer by Spk · Jul 13, 2011 at 11:43 PM
The FingerGestures asset store package has a sample that does exactly what you're describing. Check it out here: http://forum.unity3d.com/threads/95983-FingerGestures-Robust-input-gestures-at-your-fingertips!
Your answer
![](https://koobas.hobune.stream/wayback/20220613062951im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vector3 and power 1 Answer
A node in a childnode? 1 Answer
Make objects easier to select 1 Answer
Can I stop a lerp completely when it gets to 20% of its original value? 3 Answers
Make a cube bounce off four walls 1 Answer