x


GameObject following touch input, gesture and speed?

I'm curious about replicating the following behavior in Unity:

http://www.youtube.com/watch?v=syy2xt3CwHg

When the finger is in a specific area of the screen, a game object is spawned. It follows the finger gestures and when the finger is released, the gameobject is deattached. If the finger is release with previous speed (aka swipe) the gameobject is thrown.

Can i do this with the stock library or should i search for something in the Unity Store?

more ▼

asked May 28 '12 at 01:47 AM

vinny2 gravatar image

vinny2
46 9 9 10

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

1 answer: sort voted first

Stock library is perfectly fine for that. Just use the regular Input.touches[]. Though I would avoid using touchPhases since they are somewhat slow and unreliable. Either keep track of the pressed/released state yourself or for something a bit more fancy, you could use http://technology.blurst.com/iphone-multi-touch/ .

more ▼

answered May 28 '12 at 02:26 AM

hathol gravatar image

hathol
1.6k 4 11

Do you know any examples of this behavior or something similar?

May 28 '12 at 03:16 AM vinny2

Let's see...

GameObject yourObject = null;
bool hasTouched = false;
Vector2 lastPos = Vector3.zero;

Update()
{
    if(!hasTouched && Input.touchCount == 1) // pressed
    {
        hasTouched = true;
        lastPos = Input.touches[0].position;
        yourObject = Instantiate(someObjectPrefab, lastPos, Quaternion.Identity)
    }
    else if(hasTouched && Input.touchCount == 1) // finger down
    {
        lastPos = Input.touches[0].position;
        yourObject.transform.Position = Camera.main.ViewportToWorldPoint(lastPos);
    }
    else if(hasToched && Input.touchCount == 0) // released
    {
        Vector3 direction = Input.touches[0].position - lastPos;
        yourObject.Rigidbody.velocity = direction.normalized*someSpeedValue;
        hasTouched = false;
    }
}

This is untested and 10-min-hack-something-together code (I haven't Unity installed an that machine here), but it should give you an idea of what you're after

May 28 '12 at 03:44 AM hathol
(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:

x2013
x981
x606

asked: May 28 '12 at 01:47 AM

Seen: 1465 times

Last Updated: May 28 '12 at 03:45 AM