I thought there would be all kinds of examples of how to move an object around in the scene using Touch, but I couldn’t find anything that was simple and straightforward for someone just starting out with Java (like me!).

After digging around in a bunch of different scripts and reading thru a ton a posts on the Unity Forum, I found this post by col000r which had a nice, simple example. I simplified it a little bit so it would work with just 1 attached object.

1
2
3
4
5
for(touch in iPhoneInput.touches) {
     if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
          transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 10));
     }
}

That script gets a good bit of the job done, but the object will jump to the position of you finger no matter where you touch the screen.  I wanted the object to only move if you touch it directly.  So I added in the raycast code from my previous Unity iPhone Touch Animation Tutorial (which I found in the iPhone-Match example project).

1
2
3
4
5
6
7
function Update () {
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     if (Physics.Raycast (ray, hit, 100)) {
          //do something here
     }
}

TouchMove Mini-Tutorial:

You can download the Tut2-TouchMove.zip project before you get started to see the completed tutorial or you can follow along with the steps below. This one is pretty simple, so following the steps won’t take long.

1. Create a cube: Go to Game Object –> Create Other –> Cube

2. Add a Box Collider to the Cube: Go to Add Component –> Physics –> Box Collider (if your cube doesn’t already have one)

3. Create a new script: Go to Assets –> Create –> Javascript.  This will create a new script in your Project panel named NewBehaviourScript.  Double-click the script in your Project panel to open it in Unitron.

Copy the following code, paste it into the file, save, and then switch back in Unity.  Drag-and-drop the script from the Project view onto the cube in either the Hierarchy or Scene panel.

1
2
3
4
5
6
7
8
9
10
11
12
 
function Update () {
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     for(touch in iPhoneInput.touches) {
     	if (Physics.Raycast (ray, hit, 100)) {
	     if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
	          transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 10));
	     }
	}
     }
}

4. Be sure to set your iPhone Bundle Identifier: Go to Edit –> Project Settings –> Player and then give it a domain and name.  See Step 16 in the Unity iPhone Touch Animation Tutorail if you need help with this step.

5.  Build and Run: That should do it.

Note that if you have multiple objects with Colliders on them, your cube will move when you click on any of them (bug!).

Feel free to post in the comments improvements to the above script and/or if you have suggestions for how to handle any of the following…

Planned improvements:
– Object should only move if you touch it, not other Mesh Colliders.
– Object should collide with other objects while being moved
– Object should continue to move after you lift your finger, slow over time — like a hockey puck.
– Object should bounce off other objects
– Move multiple objects independently

7 Responses to “Moving An Object With Touch”

  1. drahn Says:

    Meanstreak,

    Have you worked at all on your wish list regarding moving an object via touch? The reason I ask is I have been working on the exact same thing and running into an issue on determining “momentum”. I have tried various solutions but nothing seems to give me that feeling of true physics - I am looking to fling the object after I let go and I cant seem to come up with the right feel. Have you played with this since?

    all the best,

    drahn

  2. buy_vigrxplus Says:

    Pretty cool post. I just stumbled upon your blog and wanted to say
    that I have really liked reading your blog posts. Anyway
    I’ll be subscribing to your blog and I hope you post again soon!

  3. Thanks Says:

    Thanks so much, this is really helpful. This super simple approach helps newbies like me to understand the mechanic. I wish there was more documentation like this and the great stuff (for java) at Processing.org

  4. Mango! Says:

    Hi
    This is closer to what I’m looking for.

    However, I’m wondering if your code will do the following for me:

    1. Player touches the OBJECT on screen and DRAGS their finger over to the point where they want the object to stop.

    2. As the player is doing this the OBJECT is moving at “x” speed and will continue to move to the endpoint even if the player has lifted their finger from the screen. Along the way, the OBJECT will avoid collision with other objects in its path.

    Are you able to help point me in the right direction for this? I’m a noob! Cheers

  5. Mango! Says:

    Hi again
    I followed your tutorial word-for-word, and even when I open your completed project and build, none of this works for me…

    I touch the object, nothing happens. Something tells me it’s probably something stupid and obvious but if your example doesn’t work either, well *shrug*…
    Help!
    M

  6. Mango! Says:

    I was right - it was something stupid and all is well.

    How is your research going on:
    Planned improvements:
    – Object should only move if you touch it, not other Mesh Colliders.
    – Object should collide with other objects while being moved
    – Object should continue to move after you lift your finger, slow over time — like a hockey puck.
    – Object should bounce off other objects
    – Move multiple objects independently

    I’m looking to do same myself and will keep researching. If I find anything useful that works I’ll post it here

  7. Dimitris Says:

    If you want to have more than one draggable objects, just use this instead:

    function Update () {
    var hit : RaycastHit;
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    for(touch in iPhoneInput.touches) {
    if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
    if (Physics.Raycast(ray, hit, 100) && hit.transform == transform) {
    transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 10));
    }
    }
    }
    }

Leave a Reply