Creating a movable UI button/image?

I recently joined a small Android game dev team, as a way to get more hands on experience with using Unity for game development. Recently I was given the task of rigging up touch screen controls for the player. Now this isn’t a horribly difficult task as, being our first build was on PC, and not Android, the script and stuff for moving the player is already written up, I just need to whip up a UI button that activates that script when the “movement pad” is touched.

However, the “movement pad” also needs to be able to move around the screen. Say the player wants to be able to drag the movement pad from being on the left end of the screen to the right end. How could this be accomplished?

So in other words, if the movement pad is on the left hand of the screen, thus being controlled by the players left thumb, but he wants it controlled by the right thumb, he can drag the movement over to the right side, so that his right thumb can use it.

Instead of using the movement pad to move UI elements, why not just let them directly drag UI elements?

  • Give each UI element that you want move-able a variable that’s their positional offset on the screen.
  • Make it so when the player touches that UI element, it starts the dragging process and stores the current touch position.
  • In Update (if dragging is true), take the current positional offset of that UI element and add to it the current touch position minus last frame’s touch position. Make sure to update the previous touch position variable to the current position after this, of course. That way each frame it moves the UI however much the player’s finger moved.
  • When the player lets go, set dragging back to false.