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 BongWater · Dec 16, 2015 at 01:16 PM · androidbuttontouchjumpsensitivity

buttons vs touch.

Hey guys, well basicly I'm making an endless runner for android but I'm having a bit of a problem, when I play on the pc everything is perfect, but once I've made a build and test it on the android theres problems. Basiclly it seems that the touch is weaker than the button, my player cant jump, he moves like mm of the floor, but onn the pc its fine. I have jumpForce set to 2, should i keep.changing it and re-building until it woerks.fine.on touch or is there another solution?

 using UnityEngine;
 using System.Collections;
 
 public class Player_Controller : MonoBehaviour {
 
     public float jumpForce = 1.0f;
     
     // Update is called once per frame
     void Update () {
 
         int fingerCount = 0;
         foreach (Touch touch in Input.touches) {
             if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                 fingerCount++;
 
         }
         if (fingerCount > 0) {
             Debug.Log ("touch"); //debug works
             JumpUp ();
         }
     
         if (Input.GetButton ("Jump")) {  
             JumpUp ();
         }
     }
 
     void JumpUp () {
     
         GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpForce));
     }
 
 }
 

Comment
Add comment · Show 2
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 wibble82 · Dec 16, 2015 at 01:41 PM 0
Share

We will need to see code to answer this question. Without knowing what you're doing, nobody knows what 'jumpForce' is, how it is applied to whatever you're moving, or how you're reading the controls and applying them to your object.

avatar image BongWater wibble82 · Dec 16, 2015 at 04:07 PM 0
Share

done, I was on my phone when I asked the question so didn't have code on me

1 Reply

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

Answer by wibble82 · Dec 16, 2015 at 04:46 PM

Interesting.

Assuming this really is all that's going on, there shouldn't be any difference that comes from the controls.

I think the issue comes from 2 problems:

  • Your JumpUp is called every frame while the button is held (presumably on purpose)

  • It is called from within the 'Update' function

The Update function is tied to the render frame rate of your game, which may well be different on pc to mobile. Additionally it has nothing to do with the actual physics (FixedUpdate).

Right now, when you have jump held:

  • Every Update step, you apply a force to the object to push it up

  • Every FixedUpdate step, the physics engine applies gravity to push it down

As a result, if you are doing 60 fixed updates per second, and on PC you are doing 60 updates per second, but on mobile only 30 updates per second, then your jump will seem twice as weak on mobile.

The correct solution would be to apply your per frame force inside the FixedUpdate function:

 public class Player_Controller : MonoBehaviour {
  
      public float jumpForce = 1.0f;
      bool thrusting = false;
      
      // Update is called once per frame, and is linked to rendering and input events
      void Update () {
 
          //clear our thrusting boolean
          thrusting = false;
  
          //check fingers
          int fingerCount = 0;
          foreach (Touch touch in Input.touches) {
              if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                  fingerCount++;
  
          }
 
          //mark as thrusting if pressing a finger or holding the Jump button
          if (fingerCount > 0) {
              thrusting = true;
          }
          if (Input.GetButton ("Jump")) {  
              thrusting = true;
          }
      }
 
      // FixedUpdate is called once per physics step, and we should do any physics dependent operations here
      void FixedUpdate() {
          //if we should be thrusting, apply an upwards force
          if(thrusting)
             GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpForce));
      }
 
  }

it'd be well worth reading up on the difference between Update and FixedUpdate - do a google and there's lots of info on them. They're quite important concepts especially when you start doing physics stuff like your jumping.

-Chris

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 Arshia001 · Dec 16, 2015 at 06:13 PM 0
Share

Let me add that it's generally a bad idea to use physics for a character that's not moving according to physics such as a human.

avatar image BongWater · Dec 17, 2015 at 07:48 PM 0
Share

works perfectly, tank you! I'll definatly have a look at the differences between update and fixedUpdate, could help me out in the future

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

How do I transfer the control to the PC on your android? how to change the script the character animation that would work? what should be done? 1 Answer

UI button events not working on android 3 Answers

Unity game doesn't detect every click 0 Answers

How to get access to bool from one script to another with touch input? 1 Answer

Hold touch button Unity Javascript 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