Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Anonymous 1 · Jun 04, 2011 at 07:38 AM · androidmovementiphonetouchcontrol

Help with iphone/android touch controls

Hello i am learning android development i created a simple 2D car game with left arrow used to go left and right arrow to right. I use these commands

if (Input.GetKey(KeyCode.LeftArrow))

now that i am successful with it in pc i want it to use with my android. I created 2 guiText '<' for left and '>' for right i got them on the screen but i dont know how to implement them in my script and use it like 'player touches '>' for right' .. please help

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by kariwmklawm · Jun 04, 2011 at 10:43 AM

Hi There!

For your entertainment and mine I created a simple script which should implement touch controls for phones (I don't have a mobile phone!)

Oh, also, whilst you 'could' use GuiText, a guiTexture is much more easier to use for mobile input (although neither is preferable!) Regardless, here's the code:

 using UnityEngine;
 [RequireComponent(typeof(GUITexture))]
 public class UnityBasic1 : MonoBehaviour
 {

 private GUITexture _gui;
 public Vector2 MousePosition;
 private bool _buttonEnabled;//This means this button can only be 'pressed' once a frame.

 void Start()
 {
     transform.localScale = Vector3.zero;//for some retarded reason gui elements scale from 0 not 1.
     _gui = GetComponent<GUITexture>();
     _gui.pixelInset = new Rect(_gui.pixelInset.x, _gui.pixelInset.y, _gui.texture.width, _gui.texture.height);//make sure it's pixel perfect sized.
 }
 void Update()
 {
     _buttonEnabled = false;//We reset the button.

     foreach (Touch touch in Input.touches)
     {
         if (_buttonEnabled) return;//don't go any further if this button is pressed.
         if (!_gui.HitTest(touch.position)) continue;//do hit test, we 'press' it??
         switch (touch.phase)
         {
             case TouchPhase.Began:
             case TouchPhase.Moved:
             case TouchPhase.Stationary:
                 ButtonPressed();//We hit the button!
                 return;//Return completely out of the update.
         }
     }

     //NOTE: PC TESTING
     if (_buttonEnabled) return;//don't go any further if this button is pressed.

     MousePosition = Input.mousePosition;
     if (!Input.GetMouseButton(0)) return;//Bugger off if no mouse press.
     if (!_gui.HitTest(MousePosition)) return;
     ButtonPressed(); //We hit the button!
 }

 private void ButtonPressed()
 {
     _buttonEnabled = true;//This button has been hit.
     Debug.Log("Do incremental action here.");
 }
 }
Comment
Add comment · Show 8 · 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 Anonymous 1 · Jun 04, 2011 at 03:58 PM 0
Share

dude i will sure check this when i get home , this community is best!!

avatar image Anonymous 1 · Jun 05, 2011 at 03:47 AM 0
Share

can u explain me how it works ?

avatar image kariwmklawm · Jun 06, 2011 at 12:10 AM 0
Share

Hi, it uses a GuiTexture (1 drawcall per texture on screen btw), and uses the in-built function HitTest (for the GuiTexture) reading if you have your finger is on a point. If your finger is on a point inside a guiTexture (this includes any alpha around the edges of an opaque button), it will trigger a repeating (every update) function -- in this case ButtonPressed. Touches for a phone are read as Input.touches, which is an array that collects every touch you do. Each touch has different modes, and we're only interested in touches that are currrently on the screen. Each important line of code also explains what that line does.

Please be more precise if something is confusing in particular.

avatar image Anonymous 1 · Jun 06, 2011 at 04:18 AM 0
Share

thanx for your help but ( i am a little new ) how do implement this in my script ? like i said i use " if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))" how can i replace your code with $$anonymous$$e ? thanx

avatar image Anonymous 1 · Jun 10, 2011 at 11:58 AM 0
Share

anyone ?? i am suffering

Show more comments
avatar image
0

Answer by awesomeface · Aug 03, 2012 at 04:50 PM

how do you use this code to rotate a sphere on ios when the user drags their finger around the gui texture

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How to make camera position relative to a specific target. 1 Answer

How to drag object along with dragging touch? 1 Answer

How to make a character move towards a side of the screen that's pressed at a constant rate? 2 Answers

Touch Hold for Power 1 Answer

I need to move an object when touched on an android phone 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