Sunday, September 21, 2014

Unity Tip: Easy 2D Drag with Touch

This is a quick and incredibly simple way to touch and drag gameobjects in 2D unity space.
______________________________________________________________

The Script!



For Copying

using UnityEngine;
using System.Collections;

public class TouchDrag : MonoBehaviour {

// Update is called once per frame
public float speed = 0.1F;
      void Update() {
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
                 Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
                 transform.Translate(touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
            }
      }
}

     
______________________________________________________________

"Why Post this Tiny Script?"



Simple. A lot of people have asked. Not me personally, but while researching in the forums I found tons of questions about how to drag with touch. Every game has special requirements of its code, but I found a lot of the answers that, while functional, were really complicated. Many didn't have answers at all. Thus, when I discovered that I had a totally simple and functional drag script: Sharing! :D


______________________________________________________________

"How Should I Use It For My Game?"


  • Experiment
    • I suggest testing out the speed variable, I haven't yet
  • Bumpers
    • These will keep your object from flying off the screen at flicking-speeds
    • 2D colliders on object and bumper-object
    • I suggest a 2D Rigidbody set to "is kinematic" on the bumper
  • It's Your Game
    • Do what you want. If it's really cool, blog about it!


______________________________________________________________
"Where did you find it?"
In the documentation. That's right. It's right there. It's the first example under Input.GetTouch. The only problem? It's not labeled at all! I didn't actually even know I had a great touch-drag script until I realized I WAS touch-dragging an old gameobject that still had this TEST script thrown on as a beginner's try at touch!

______________________________________________________________

Serendipity for All! :)

My happy accident is now everybody's happy accident! Let us rejoice and move things around with our fingers!

______________________________________________________________


All blog posts by UnityGirl are inspired by my work with Brunelleschi: Age of Architects on the Aesop Games team. Check out our Crowd Funding page(s)!




5 comments:

  1. Where do I attach this script? To the gameObject that is being dragged?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Great stuff, thanks for sharing. I have a problem though, say i want to use this script on multiple objects in the same scene and have them moving independently, how would i do that? (i'm trying to build a pong for android and if i attach this script to the two player/paddles they move at the same time, basically it only responds to one touch.)

    ReplyDelete