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