x


Dragging an object in iOS (UnityScript)

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)

more ▼

asked Jul 12 '11 at 10:44 AM

hawken gravatar image

hawken
91 19 24 25

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;
       }
    }
}
more ▼

answered Jul 13 '11 at 07:35 AM

hawken gravatar image

hawken
91 19 24 25

(comments are locked)
10|3000 characters needed characters left
more ▼

answered Nov 03 '12 at 12:40 PM

efge gravatar image

efge
5.1k 5 14 38

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 03 '12 at 01:09 PM

Anxowtf gravatar image

Anxowtf
1.6k 22 27 37

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1963
x1703
x1529
x580
x55

asked: Jul 12 '11 at 10:44 AM

Seen: 1669 times

Last Updated: Nov 03 '12 at 01:09 PM