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 Ranger_Steve · May 11, 2012 at 01:06 AM · androidinputcontrolstouchscreen

Android Input

Hey, I have been trying to get touch screen controls working for awhile now. But I just can't get it to work the way I want it to. I have one gui button at the moment to move to the right, but when I touch the button the cube only moves slightly and I have to keep pressing the button to move any further. Is there away to get this to move constantly when I touch down on the button and then stop when it's not touched? the only way I got this working was by using the Input.touchCount >= 1 but that will only work for one direction can anyone help me? please.

here is my code after a few attempts

 //Vectors for movement directions
 private Vector3 left,right;

 //Float for speed
 private float speed;

 //Boolean to check if button is touched
 private bool touched;

 //Setting variables initial values
 void Start () 
 {
     touched = false;
     speed = 2f;

     //Values for movement along the X-axis
     left = new Vector3(-speed,0,0);
     right = new Vector3(speed,0,0);
 }
 
 void Update ()
 {    
     //calling the movement method
     applyMovement();
 }
 
 private void OnGUI()
 {
     //If the button and the screen is touched set the touched boolean to true 
     if(GUI.Button(new Rect(10,10,150,150),"RIGHT") && Input.touchCount >= 1)
     {
         touched = true;
     }
     
     else
     {
         touched = false;
     }
 }
 
 private void applyMovement()
 {
     if(touched)
     {
         //apply positive movement along the x-axis when touched
         transform.Translate(right);
     }
 }
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 alpaca of zion · May 12, 2012 at 05:45 PM

GUI.Button(new Rect(10,10,150,150),"RIGHT") only returns true when the button is first clicked.

One possible solution would be to skip the OnGUI and make actual game objects the user can click. I'm doing this with a second camera showing a gui layer. In the Update() function your code, attached to the button gameobject, would look something like:

     // Loop through the touches looking for a finger on the right button.
     for (int t = 0; t < Input.touchCount; t++)
     {
         if (Input.touches[t].phase == TouchPhase.Began || 
             Input.touches[t].phase == TouchPhase.Moved || 
             Input.touches[t].phase == TouchPhase.Stationary)
         {
             inputPosition = Input.touches[t].position;
             Ray ray = guiCamera.ScreenPointToRay(inputPosition);
             RaycastHit hitInfo = new RaycastHit();
             if (Physics.Raycast(ray, out hitInfo, 100))
             {
                 if (hitInfo.collider.gameObject == this.gameObject)
                 {
                     YourCharacter.MoveRight();
                 }
             }
         }
     }
Comment
Add comment · Show 1 · 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 Ranger_Steve · May 15, 2012 at 02:34 AM 0
Share

I can't seem to get this script to work. I don't know if it's the fact the I am referencing something wrong, but what I have done now is created a GUI Text object and added that to the script. I also added it to the main camera so ins$$anonymous$$d of guiCamera I have Camera.main but there is no movement what so ever.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

smooth movement with rigidbody2D 1 Answer

How do you map touch to one of the input axes? 1 Answer

How to allow clicking of 2 buttons at once in Android 0 Answers

Android to unity INPUT 0 Answers

How to transform PC controls to Touchscreen controls 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