Does anyone have a solution for dragging colliders in iOS? I tried the following, which I think is not far off, compiles but doesn’t work on the device.
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.Moved && Physics.Raycast(ray.origin, ray.direction,hit)) {
var touchPos : Vector2 = Input.GetTouch(0).deltaPosition; // touch position
var newPos : Vector2; //object position
newPos = touchPos;
switch(hit.collider.name){
case "object01":
Debug.Log("object01 drag");
transform.Translate(newPos);
break;
case "object02":
Debug.Log("object01 drag");
transform.Translate(newPos);
break;
}
}
}
}
Any help much appreciated It’s for detecting a drag on any object using a collider (I’ve not got around to using tags yet)
trying a few variations on this, but really don’t know what to add for newPos. I have a mousedown version of this running just fine, looks like I’m going to have to give up on iPhone touch drag in Javascript for now… here’s the code, that functions for mouse, but not for touch;
Any clues as to what to put in the newPos var would be great appreciated!
var titleCamera : Camera;
function OnMouseDown () {
var screenSpace = titleCamera.WorldToScreenPoint(transform.position);
var offset = transform.position - titleCamera.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.GetMouseButton(0))
{
var clickPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
var newPos = titleCamera.ScreenToWorldPoint(clickPos) + offset;
transform.position = newPos;
yield;
}
}
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];
while (touch.phase == TouchPhase.Moved && Physics.Raycast(ray.origin, ray.direction,hit)){
var touchPos : Vector2 = Input.GetTouch(0).deltaPosition;
var newPos = touchPos;
transform.position = newPos;
}
}
}
It might also be worth considering buying something like “Touches” from the asset store. While you CAN write it all yourself, there really is no need unless you need to do something very different. When I got my input touches package it only cost me 10 bucks and saved me a lot of days of trying to work out controls and gave me more time to focus on game design. There are is also the more popular “Finger Gestures” package but it is also more expansive.