# Touch Input on Sprites

**URL:** <https://discussions.unity.com/t/touch-input-on-sprites/97754>\
**Category:** Questions & Answers\
**Created:** [March 1, 2014, 11:56am UTC](https://discussions.unity.com/t/touch-input-on-sprites/97754 "2014-03-01T11:56:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lixpoxx](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@lixpoxx](https://discussions.unity.com/u/lixpoxx)\
**Post date:** [March 1, 2014, 11:56am UTC](https://discussions.unity.com/t/touch-input-on-sprites/97754/1 "2014-03-01T11:56:41Z")

</div>

Hey.  
I’ve been trying to figure out how to make a control for my game.  
I got the idea to take a sprite and if the finger is on this sprite  
it should act like Input.GetKey.  
(A sprite also doesn’t change the position as the GUI, what i mean with it is that it doesn’t change its position and is easier to build it)  
Can anyone help me.

---

<div class="post-metadata">

**Author:** ![Rioneer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/rioneer/32/44243_2.png) [@Rioneer](https://discussions.unity.com/u/Rioneer)\
**Post date:** [March 1, 2014, 12:29pm UTC](https://discussions.unity.com/t/touch-input-on-sprites/97754/2 "2014-03-01T12:29:35Z")

</div>

void Update()  
{  
if (Input.touchCount == 1)  
{  
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);  
Vector2 touchPos = new Vector2(wp.x, wp.y);  
if (collider2D == Physics2D.OverlapPoint(touchPos))  
{  
//your code  
}  
}  
}

[Here’s the source.](http://answers.unity3d.com/questions/577314/how-to-detect-if-a-sprite-was-object-was-touched-i.html)

Edit :

Here’s something I wrote. It would come in handy if you need to control all touch inputs from 1 script :

```
    private GameObject beganObj;
    private GameObject currentObj;
	
	void Update () 
    {
        if(Input.touchCount > 0)
        {
            for(int i = 0; i < Input.touchCount; i++)
            {
                Touch currentTouch = Input.GetTouch(i);
                if(currentTouch.phase == TouchPhase.Began)
                {
                    Vector2 v2 = new Vector2(Camera.main.ScreenToWorldPoint(currentTouch.position).x, Camera.main.ScreenToWorldPoint(currentTouch.position).y);
                    Collider2D c2d = Physics2D.OverlapPoint(v2);
                    
                    if(c2d != null)
                    {
                        beganObj = c2d.gameObject;
                    }
                }

                if (currentTouch.phase == TouchPhase.Moved)
                {
                    Vector2 v2 = new Vector2(Camera.main.ScreenToWorldPoint(currentTouch.position).x, Camera.main.ScreenToWorldPoint(currentTouch.position).y);
                    Collider2D c2d = Physics2D.OverlapPoint(v2);                    

                    if (c2d != null)
                    {
                        currentObj = c2d.gameObject;
                    }
                }
            }
        }
	}

```

JS :

```
    function Update()
    {
        if (Input.touchCount == 1)
        {
            wp : Vector3 = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            touchPos : Vector2 = new Vector2(wp.x, wp.y);
            if (collider2D == Physics2D.OverlapPoint(touchPos))
            {
                //your code
            }
        }
    }

```

I think this will do it in javascript. Attach this to sprite you want to use as button and make sure you have a 2D collider in your sprite object.

Another way to do this with guiTextures :

```
function Update()
{
   if(Input.touches.Length > 0)
   {
      for(int i = 0; i < Input.touchCount; i++)
      {
         if(this.guiTexture.HitTest(Input.GetTouch(i).position)
         {
            if(Input.GetTouch(i).phase == TouchPhase.Began)
            {
               // Write what you want to do when guiTexture is touched.
            }
         }
      }
   }
}

```

Attach this script to your gameObject that’s holding guiTexture. I haven’t been using javascript for a while now, so I apologize in advance if there are any mistakes. Good luck.
