- Home /
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?
Answer by hathol · May 28, 2012 at 02:26 AM
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/ .
Do you know any examples of this behavior or something similar?
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-$$anonymous$$-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
Your answer
Follow this Question
Related Questions
Issue with Input.GetMouseButton (0) on mobile (iPhone) 2 Answers
Input.GetAxis("Vertical") on touch devices. 2 Answers
iPhone touch events slowdown fps?!? 2 Answers
Iphone touch not working 0 Answers
Delay before first touch position changes on iPhone 1 Answer