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 /
  • Help Room /
avatar image
0
Question by koothur19021 · Dec 30, 2015 at 09:07 AM · androidtouch controlstouchscreeninput.touch

Need help with touch controls for an android game.

Hello guys I am new to unity and I am making a game where there will be two bouncy sprites spawning from either side of the screen according to the touch on both sides. I was able to make the bouncy sprite spawn on tap at both sides of the screen ( I have divided the screen into two touch areas , left sprite spawn when left side is touched and right is spawned when the right is touched). Below is the code for left paddle :

 var Leftpaddle : GameObject;
  
  function Update () 
  {
      if (Input.touchCount>0)
      {
      var touch : Touch = Input.GetTouch(0);
      if(touch.position.x < Screen.width/2)    
      {
       this.GetComponent.<Renderer>().enabled = true;
       this.GetComponent.<EdgeCollider2D>().enabled = true;
          }
          }
      else
      {
       this.GetComponent.<Renderer>().enabled = false;
       this.GetComponent.<EdgeCollider2D>().enabled = false;
      }
      if (Input.touchCount>0)
      {
      var touch2 : Touch = Input.GetTouch(0);
     // var touch3: Touch = Input.GetTouch(0);
      if( touch2.position.x > Screen.width/2 && touch2.position.x < Screen.width/2 )
      {
       this.GetComponent.<Renderer>().enabled = false;
       this.GetComponent.<EdgeCollider2D>().enabled = false;
          }
 }
 }

and this is the code for right paddle var paddle : GameObject;

  function Update () 
  {
      if (Input.touchCount>0)
      {
      var touch : Touch = Input.GetTouch(0);
      if(touch.position.x > Screen.width/2)
      {
       this.GetComponent.<Renderer>().enabled = true;
       this.GetComponent.<EdgeCollider2D>().enabled = true;
          }
          }
      else
      {
       this.GetComponent.<Renderer>().enabled = false;
       this.GetComponent.<EdgeCollider2D>().enabled = false;
      }
      if (Input.touchCount>0)
      {
      var touch2 : Touch = Input.GetTouch(0);
     // var touch3: Touch = Input.GetTouch(0);
      if( touch2.position.x > Screen.width/2 && touch2.position.x < Screen.width/2 )
      {
        
       this.GetComponent.<Renderer>().enabled = false;
       this.GetComponent.<EdgeCollider2D>().enabled = false;
          }
 }
      
 }

Now the problem is , when I touch both sides of the screen the sprites(paddles) are not getting disabled. Any one of them still spawns and when I press and hold on left / right side spawning a side and when i tap on the other side the other side spawns too making both paddles spawn on their places.

Couldn't figure out what is the problem. Any help is appreciated. Thanks

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 g__l · Dec 30, 2015 at 11:09 AM

First of all you could create two buttons for left and right and make their colour 0 in all colour selectors in the inspector, so it will be invisible. Alternativly you could use the Array Input.touches and create code which determins if a touch is left so -x or right so +x coordinate. Either should work.

Note - In the inspector only one button can be pressed at a time but when you build the game for android it will work fine.

Comment
Add comment · Show 2 · 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 koothur19021 · Dec 30, 2015 at 04:17 PM 0
Share

Your method worked. thank you for that. Can you tell me how can I make only one button work at a time when deployed to android because I don't want both buttons to work when I press both of them at the same time.

avatar image g__l koothur19021 · Jan 02, 2016 at 12:52 PM 0
Share

$$anonymous$$aybe in your functions for each buttons you could put an if statement around the code. Then make a single variable (bool) that goes true when a button is pushed. Then in your if statement make it so that the code is only completed if the variable is false (one button is not pushed already, so you get the desired result of one button being pushed at a time. Hope it helps.

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

42 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 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

Touch screen not working on Android build? 0 Answers

Rotate player with swiping on android device 0 Answers

Vector3.back not working 2 Answers

How to replace KeyCode.Space with a simple tap on Android? 1 Answer

How can convert keyboard controller to touchscreen(android .ios ...etc) 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