Unity – Selecting Game Object by Touch and Moving with Finger

device-2014-03-27-124634Here is an example of how to select a game object by touch using Unity 3d’s Physics Raycast. The first part is to find the game object that the user is touching with their finger. This is done by a raycast.  If that is all you want to do then you can skip the inner ‘if statement.’ The next step is to get the finger’s position in pixels and then convert it to game world coordinates.

The setup: 

I placed my box (“paddle”) at the origin (0, 0, 0) and the camera at (0, 0, -10). I have not tried at different position but you may have to mess with the ‘z’ coordinate in the code to get it working. You do not have to have the background. Here is a screen shot of my setup:

Screen Shot 2014-03-27 at 2.53.25 PM

The code: 


#pragma strict

function Update () {
  if(Input.touchCount > 0)
    var hit : RaycastHit; 
    /** position in pixels **/
   var finger = Vector3(Input.GetTouch(0).position.x,
                            Input.GetTouch(0).position.y, 10.0f); //fix z here  
    var ray : Ray = Camera.main.ScreenPointToRay(finger);
    var touchPosition: Vector3;
    var position: Vector3;
    var touch : Touch;
    touch = Input.GetTouch(0);

    /** For debugging, user your own game object name **/        
    Debug.Log("Paddle Position " + GameObject.Find("Paddle").transform.position);            
    if (Physics.Raycast(ray, hit, Mathf.Infinity)) { // hit an object

       if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved){ //finger is moving

         /** position is game world **/                             
         position = Camera.main.ScreenToWorldPoint(finger); 

         /** if you want a trailing object: z is 0 to 1.0 **/
         touchPosition =  Vector3.Lerp(hit.rigidbody.position, position, 0.9f);

         /** Object that was hit **/
         hit.rigidbody.transform.position = touchPosition; 

         /** Debugging **/
         Debug.Log("finger " + finger);
         Debug.Log("position " + position);
         Debug.Log("touchPosition " + touchPosition);
         Debug.Log("paddle " +  hit.rigidbody.position);

Posted by in Coding


Permalink 1 Comment

One response to “Unity – Selecting Game Object by Touch and Moving with Finger

  1. mohannd tabbakh

    “NullReferenceException: Object reference not set to an instance of an object

    the word finger.Update () (at Assets/scripts/the word finger.js:26) ” i have this proplem

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on
Fabulou wholesale Petr Mrazek jersey is back. Best choice to get the wholesale Petr Mrazek jersey is suitable for all style of people.