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
1
Question by TheBlueSky · Mar 18, 2014 at 06:53 PM · android2dtouch

How to correctly detect touch and apply single action?

Hello everyone,

I have a game object (in a 2D game) that will jump when the space bar is hit (on PC and so) or when the screen is touched (on mobile devices and so).

When I tried the game on PC, the game object jumps as expected. However, when I get my game on an Android device, my game object jumps multiple times higher!

To debug my code I introduced some variable that counts the number or requested jumps. The number was always 1 on PC (no surprise here) and to my surprise it was almost always 2 on Android.

Below is the code on my game object:

 private void Update()
 {
     // Make the player jumps
     if ((Input.GetKeyDown(this.JumpButton) || Input.touchCount == 1) && this.isGrounded)
     {
         this.didJump = true;

         // Just for debugging
         this.totalJumpOrders++;
         this.maximumJumpOrders = this.maximumJumpOrders < this.totalJumpOrders ? this.totalJumpOrders : this.maximumJumpOrders;
     }
 }

 private void FixedUpdate()
 {
     this.isGrounded = Physics2D.Linecast(this.transform.position, this.groundCheck.position, 1 << LayerMask.NameToLayer("GroundLayer"));

     if (this.didJump)
     {
         this.didJump = false;
         this.totalJumpOrders = 0;

         this.rigidbody2D.AddForce(this.transform.up * this.JumpPower);
     }
 }

My questions are:

  1. How to correctly detect touch and apply single action? It seems like the force is applied multiple times (even more than twice from the height of the jump)

  2. How to detect that the player is still touching the screen so do not do anything until they remove their finger and touch again?

  3. Is their any guidelines on how to handle touch events with Unity?

Bear with me, I'm still new to Unity and still finding my way around :)

P.S. I tested the game on a Windows 8 machine and on Android 4.2 running on the original Samsung Galaxy S (CyanogenMod ROM)

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
6

Answer by ForceAlex · Jul 04, 2014 at 10:06 PM

Hi! Your problem is that you are checking always if there is a touch, so every frame it restarts the didJump variable to true and causes it to keep jumping.

you should check if the touch is in its beginning phase:

 if(Input.TouchCount == 1)
 {
     if(Input.GetTouch(0).phase == TouchPhase.Began)
     {
         //your code here will be called only once per touch
     }
 }

you can check here for more info on touches

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 Content1of_akind · Apr 12, 2019 at 08:30 PM 0
Share

for some reason it's doing more than one frame at a time when I put the exact code. Any Advice on how to fix it?

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

How to properly implement snake style controls for android? 0 Answers

How to continuously detect the exact location of a touch on Android? 1 Answer

Raycast Touch Android not working JS 2 Answers

Game Object On Touch Event on Android-iPhone 2D 4 Answers

Camera swipe AND gameObject touch (Android) 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