- Home /
Using touch doesn't get the correct vector.
This is my code when I move my finger to move the object it does not follow my finger instead it goes right of the iphone screen too far. How do I make this object follow my finger?
var speed : float = 0.1; var detectingMotion : boolean; var arr : Array = new Array(); private var hit : RaycastHit; private var touchDeltaPosition:Vector2; private var touchedObject : Transform;
function Update () { for(touch in iPhoneInput.touches) { ///make one if statement if(iPhoneInput.touchCount == 1){ // detecting a one finger touch input if( touch.phase == iPhoneTouchPhase.Began){ //casting a ray from the touch location into the game
///create an ray for finger hit
//var detectRay : Ray = Camera.main.ScreenPointToRay(touch.position);
var detectRay : Ray = Camera.main.ScreenPointToRay(Vector3( touch.position.x, touch.position.y ));
if (Physics.Raycast (detectRay, hit)) {
print("You are touching the object "+ hit.transform.position);
///start the motion off
detectingMotion = true;
touchedObject = hit.transform;
}
}
if( touch.phase == iPhoneTouchPhase.Moved && detectingMotion == true)
{
// Get movement of the finger since last frame
touchDeltaPosition = iPhoneInput.GetTouch(0).deltaPosition;
// Move object across XY plane
touchedObject.Translate (touchDeltaPosition.x, touchDeltaPosition.y, 0);
///record the movements
print("You are moving the object y:" + touchDeltaPosition);
//print("You are moving the object y:" + touchDeltaPosition.x + " y:"+ touchDeltaPosition.y);
//add the poitions to the array.
arr.Add (touchDeltaPosition);
}
if( touch.phase == iPhoneTouchPhase.Ended)
{
///stop recording
//print("You let go" + touchedObject.position);
detectingMotion = false;
///print out movement array
for (var pos : Vector2 in arr)
{
print("moving this object after finger off"+pos.x+ ":x "+pos.y+":y");
// Move object across XY plane
//touchedObject.Translate (pos.x * speed, pos.y * speed, 0);
}
}
}
}
}
Answer by Tetrad · Jun 19, 2010 at 05:05 PM
You have to make sure your coordinate systems are correct and consistent.
In particular, these two lines will need to be adjusted.
// Get movement of the finger since last frame touchDeltaPosition = iPhoneInput.GetTouch(0).deltaPosition;
// Move object across XY plane touchedObject.Translate (touchDeltaPosition.x, touchDeltaPosition.y, 0);
deltaPosition is in screen space. Translate
moves the object in local space. I don't know how your camera and scene are set up, but if you move your finger right 20 pixels on the screen, you're telling your game object to move positive on its X axis by 20 world units.
That's most likely wrong in all cases, unless you have some kind of orthographic camera that 1 pixels = 1 unit. And always wrong if your camera is rotated. Basically the concept itself is wrong.
If you want to move the object to where your finger is pointing to in the game world, you'll probably want to do something like casting a ray using Camera.main.ScreenPointToRay
, and placing your dragging object at the location returned from Physics.Raycast
. If you do that you have to make sure you're not detecting a collision with the object you're dragging (otherwise it will move up every frame if it has any height to it at all). You can do that with layer masks, or use Physics.RaycastAll
and just skip the collision result with the object you're dragging.
thanks for your suggestion have you got a example of this ?
Just do a search for "drag gameobject", there are a lot of questions on the same subject.
If there are, they are not easy to find. A search for "drag gameobject" on this site shows one question about dragging rigidbodies on Unity for iPhone, but the answer references a Wiki code snippet which is not for the iPhone and after several hours worth of try, I haven't gotten it to work in Unity iPhone 1.7. So it seems like a code example for this answer would be very helpful.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062506im_/https://answers.unity.com/themes/thub/images/avi.jpg)