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);
}
}
}
}
}