Creating a draggable object - and defining areas.

Im new to unity, so far I created “flappy bird” like game, and I want to make something more advanced.
Im trying to create “stay in the line” 2d game, but with a draggble ball, which you need to drag in the “route” without hitting the walls.

Im not sure how to approch the two main parts in the game.

1.Creating a draggble (by touch) object.

2.Creating the route - creating the outer walls and then use “onCollsion” - is that the correct way?

and more genreal question, if im just planning on creating 2d-games, do you think that I should stick with unity?


There are a variety of drag and drop scripts posted on UA. But for what you are doing here, some have issues. Given the game mechanic, something simple might work better for you:

#pragma strict

var follow : boolean = true;

function OnMouseDown() {
	follow = true;	

function Update() {
	if (Input.GetMouseButtonUp(0)) {
		follow = false;
	if (follow) {
		var v : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		v.z = 0.0;
		transform.position = v;

Basically if you touch the ball anywhere, it snaps to the mouse position and follows until the mouse button is lifted. If you are going to be using Physics for your app, then you what the commented out line instead of the one above it. For touch, if you restrict the player to one finger, then there are just a few lines changed/added to the script.

Which brings me to your second part of your question. You could put colliders on the background and use OnCollisionEnter() or OnTriggerEnter(). This is a typical Unity way of dealing with collisions. But assuming touching the edges means death, I’d likely not do colliders and instead read the pixels of the texture(s) directly to determine if the ball is still on the path. Reading pixels directly is not a simple process, and the particulars will depend a bit on how you build and animate your paths: single texture on a moving plane, multiple random moving tiles swapped in, single using mainTextureOffset to animate, etc. But I think it would greatly simplify the path creation process.