Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lixpoxx · Mar 01, 2014 at 11:56 AM · guiinputspritetouch

Touch Input on Sprites

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nick4 · Mar 01, 2014 at 12:29 PM

 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.

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lixpoxx · Mar 01, 2014 at 08:09 PM 0
Share

I am not very familiar with C# (I prefer JavaScript), and i want to add a second button so how could i add the button with a new variable

avatar image Nick4 · Mar 01, 2014 at 09:17 PM 0
Share

I have edited my question. See if that helps.

avatar image ShrishGour · Jul 28, 2014 at 05:57 PM 0
Share

how to control the movement speed of the object from the script mentioned above ??

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why am I having UI Clickthrough in Builds with 5.2? 0 Answers

how to a make a touch button open a menu while finger is down? 1 Answer

Main menu touch input for android 1 Answer

HOW TO MAKE A 2D OBJECT MOVE WITH TOUCH INPUT ON ANOTHER 1 Answer

Coordinates of Input.GetTouch(0).position vs Screen coordinates? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges