• 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 Trede87 · Jul 24, 2014 at 08:20 PM · androidbuttontouchtouchscreen

Problem with touch button (when moved)

I got this script for android intput it works fine but the problem comes when the player holds the button and then slides the finger out of it; the button stays pressed even tho the finger is not over it, it stays pressed even when the user doesnt have a finger over the screen(after the sliding thing). How may I solve this issue? Any idea?

public Texture2D pressed, notPressed; public Rigidbody2D Player;

     void Update(){
         this.guiTexture.pixelInset = new Rect(0, 0, Screen.width/3.5f, Screen.height/6f);
 
         SpeedController playerScript = Player.GetComponent<SpeedController>();
         if (Input.touches.Length <= 0) {
             playerScript.move = 0;
         } else 
         {
             
             for (int i= 0; i < Input.touchCount; i++) {
                 
                 if (this.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     if(Input.GetTouch(i).phase == TouchPhase.Began){
                         playerScript.left = true;
                         this.guiTexture.texture = pressed;
                     }
                     if(Input.GetTouch(i).phase == TouchPhase.Ended){
                         playerScript.left = false;
                         this.guiTexture.texture = notPressed;
 
                     }
                 }
                 
                 
                 
             }
         }
     }
 }
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
0

Answer by dadika4 · Jun 28, 2017 at 11:31 AM

using UnityEngine;

public class TouchInput : MonoBehaviour {

 Ray ray;
 RaycastHit hit;
 Rigidbody rb;

 public bool left;
 public bool right;
 public bool jump;
 public int speed;



 void Start()
 {
     rb = GameObject.FindGameObjectWithTag("player").GetComponent<Rigidbody>();
     speed = 1;
 }

 void Update()
 {
     if (left)
     {
         rb.AddForce(-10*speed, 0, 0);
     }
     if (right)
     {
         rb.AddForce(10*speed, 0, 0);
     }
     if (jump)
     {
         rb.AddForce(0, 10*speed, 0);
     }
     
     Touch[] myTouches = Input.touches;

     if (Input.touchCount > 0)
     {
         for (int i = 0; i < Input.touchCount; i++)
         {
             ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);

             if (Physics.Raycast(ray, out hit, Mathf.Infinity) == true)
             {
                 //touch phase has begun
                 if (Input.GetTouch(i).phase == TouchPhase.Began)
                 {
                     if (hit.collider.tag == "left")
                     {
                         left = true;
                     }
                     else if (hit.collider.tag == "right")
                     {
                         right = true;
                     }
                     else if (hit.collider.tag == "jump")
                     {
                         jump = true;
                     }
                 }
                 //touch phase has ended for buttons
                 else if (Input.GetTouch(i).phase == TouchPhase.Ended)
                 {
                     if (hit.collider.tag == "left")
                     {
                         left = false;
                     }
                     else if (hit.collider.tag == "right")
                     {
                         right = false;
                     }
                     else if (hit.collider.tag == "jump")
                     {
                         jump = false;
                     }
                 }
             }
             if (Physics.Raycast(ray, out hit, Mathf.Infinity) == false)
             {
                 left = false;
                 right = false;
                 jump = false;
             }
         }
     }
 }

}

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 dadika4 · Jun 28, 2017 at 11:31 AM 0
Share

Jup the script lacked the physics ending touch on the sprite.

avatar image
0

Answer by DGKN · Jul 24, 2014 at 08:34 PM

Try this :

     for (int i= 0; i < Input.touchCount; i++) 
     {  
         if (this.guiTexture.HitTest(Input.GetTouch(i).position))
         {
              if(Input.GetTouch(i).phase == TouchPhase.Began)
              {
                 playerScript.left = true;
                 this.guiTexture.texture = pressed;
              }
              if(Input.GetTouch(i).phase == TouchPhase.Ended)
              {
                 playerScript.left = false;
                 this.guiTexture.texture = notPressed;
              }
         }
         else 
             playerScript.left = false;
     }
         
 


If you hold down the button and release it outside the HitTest area, then playerScript.left remains always TRUE because

if (this.guiTexture.HitTest(Input.GetTouch(i).position))

doesn't apply anymore. So you need a ... = false statement to stop the movement once you're out of the Touch area.

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 Trede87 · Jul 24, 2014 at 09:22 PM 0
Share

I already did that :P but that wasnt the solution because if I do it that way I cannot press two different buttons at the same time(because if I press the second one, the bool gets false) i have donne it in away that doesnt affect the other buttons but if I touch with another finger any part of the screen that is not a GUItexture it goes false and the player stops moving, my solution is just as yours but with an "if" statement, lets see if someone else or you have a better one :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Multiple Cars not working 1 Answer

how use specific for mobile button when we have 3 ? 1 Answer

Touch Input madness 2 Answers

Drag and Swap two object using touch 0 Answers

If you help me, i make you 3d object 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