Dragging an object using touch (UnityScript)

Hi,

I was wondering how to move colliders around using the TouchPhase.Moved input.

Having a lot of problems with this conceptually. Tried various chunks of code, can get OnMouseDown working no problems.

So far I understand that I must raycast from the camera to detect which object is being touched. I have a chunk of code for this working/debugging on the handset just fine.

The real problem I’m having is trying to get the colliders to move around when you drag them! I’ve asked this in Unity Answers but theres no real clues there, also there seems to be a real absence of tutorials on this.

Any clues?

Here’s the code I’m trying, as you can see I have no idea what to put in the newPos variable:

var titleCamera : Camera;
var hit : RaycastHit;
var ray : Ray;

function Update () {
    if(Input.touchCount == 1) {
    	ray = titleCamera.ScreenPointToRay(Input.touches[0].position);
    	var touch : Touch = Input.touches[0];
        if(touch.phase == TouchPhase.Ended  Physics.Raycast(ray.origin, ray.direction,hit)){
        	
        	switch(hit.collider.name){
			    case "object01":
			        Debug.Log("object01 tapped");
			    break;
			    case "object02":
			        Debug.Log("object02 tapped");
			    break;
			}
        }
        else if (touch.phase == TouchPhase.Began  touch.phase == TouchPhase.Moved  Physics.Raycast(ray.origin, ray.direction,hit)) {
	        var touchPos : Vector2 = Input.GetTouch(0).deltaPosition; //touch position
	        [COLOR="red"]var newPos : Vector2; //object position  something?![/COLOR]
        	
        	touchPos = newPos;
        	
        	switch(hit.collider.name){
			    case "object01":
			        Debug.Log("object01 drag");
			         transform.Translate(newPos);
			    break;
			    case "object02":
			        Debug.Log("object02 drag");
			         transform.Translate(newPos);
			    break;
			}
    	}
        else if (touch.phase == TouchPhase.Ended  !Physics.Raycast(ray.origin, ray.direction,hit)) {
        	Debug.Log("tap cancelled");
        }
    }
}

Is it even possible?

I have a game prototyped with touch, drag, drop capability - maybe this will help yours. In my case right now you can touch to select a ball, drag it horizontally across the playfield, and then the physics simulation begins once a ball is dropped. I use a sphere collider to register the touch. The size of your collider directly impacts the register of the touch… In my case I had to double the size of the collider around the object to get a good touch register - this could be one thing to check. I’m using a modified version of the TouchDrag script that can be found elsewhere on this forum…
Here’s the code I’m using for this. try attaching this to a sphere with a rigidbody and a sphere collider and set the Obj in the script to the sphere. You’ll also need to check on in the Rigidbody the Is Kinematic flag. Hope this helps…

#pragma strict 
#pragma implicit
#pragma downcast 

var obj : GameObject; 
private var radius : float;
private var oT : Transform = null; 
private var selected : boolean = false; 
private var controller : SphereCollider;
private var isKinematic : boolean;
private var tapCount : int;
function Awake() { 
   if (obj) { 
      oT = obj.transform;
      iPhoneInput.multiTouchEnabled = false;

   } else { 
      var script : TouchDrag = GetComponent(TouchDrag); 
      script.enabled = false; 
   } 
} 

function FixedUpdate () { 
   if (obj) { 
      var cam : Camera = Camera.main; 
      for (var touch : iPhoneTouch in iPhoneInput.touches) { 
         var ray = Camera.main.ScreenPointToRay(touch.position); 
         var hit : RaycastHit; 
		
         if (touch.phase == iPhoneTouchPhase.Began) { 
            if (Physics.Raycast(ray, hit, 100)) { 
               if (hit.collider.gameObject == obj) { 
                  selected = true; 
                  oT.position.y = 7.0;
               } 
            } 
         } else if (touch.phase == iPhoneTouchPhase.Moved) { 
            if (selected == true) { 
               var cameraTransform = cam.transform.InverseTransformPoint(0, 0, 0); 
               var touchPosition = cam.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z)); 

               oT.position.x = touchPosition.x; 
                         } 
         } else if (touch.phase == iPhoneTouchPhase.Ended) {
         	
            if (selected == true) { 
           controller = GetComponent(SphereCollider);
          controller.radius = 0.5;
          	obj.rigidbody.isKinematic = false; 
           obj.rigidbody.WakeUp();
            selected = false;         
                     } 
      } 
   } 
}
}