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 /
avatar image
0
Question by ricardohenrique996 · Jan 20, 2016 at 03:35 PM · unity5unityeditor

Different sizes of jump when Android device

I created a game like Infinite Runner that functioning properly in the Game tab of the Unity 5 but on android devices when I do the Jump it's never had the same height.

Note: App tested on Android 4.4.2 and 5.0
Note 2: Some values are added in inspector.

Functions that make the jump

 void FixedUpdate () {
     CheckPlayerInGround();
     MakeJump();
     animator.SetBool("jump", !steppingDown);
 }
 
 void CheckPlayerInGround(){
     steppingDown = Physics2D.OverlapCircle(GroundCheck.position, 0.2f, whatIsGround);
 }
 
 void MakeJump(){
     if(Input.touchCount > 0){
         
         if((Input.GetTouch(0).position.x < Screen.width / 2) && steppingDown){
             if(slide == true){
                 UpdateColliderScenarioPosition(0.37f);
                 slide = false;
             }
             audio.PlayOneShot(audioJump);
             audio.volume = 0.75f;
             playerRigidbody2D.AddForce(new Vector2(0, jumpPower * Time.fixedDeltaTime));
         }
     }
 }
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
1
Best Answer

Answer by NoseKills · Jan 20, 2016 at 06:17 PM

If you want to get the same jump height every time, then you have to (simplifying)

  • apply the same amount of force

  • for the same amount of times / same period of time

every time you jump.

Right now you have at least 2 things in your code that might affect the jump height in different scenarios.

 // this is basically your code 
 void FixedUpdate() {
     if(Input.touchCount > 0) {
           playerRigidbody2D.AddForce(new Vector2(0, jumpPower * Time.fixedDeltaTime));
     }
 }

As said in the API docs, touchcount stays the same throughout the frame. That means "from one Update() call to the next Update() call". So that duration depends on your framerate

FixedUpdate() on the other hand runs n times per second. If your framerate drops, n FixedUpdates are simulated during 1 Update. As a result your code adds force n times depending on your frame rate whenever you touch the screen.

Another oddity that I'm spotting (although it shouldn't be causing problems in your setup) is the force magnitude calculation

 playerRigidbody2D.AddForce(new Vector2(0, jumpPower * Time.fixedDeltaTime));

If you want the jump to be of same height every time, then time shouldn't be a factor in the calculation. Of course Time.fixedDeltaTime is the same number every time (even Time.deltaTime is nowadays when used inside FixedUpdate() ) , but still I don't think it should be a factor in the formula. If you'd now tweak your physics simulation rate, fixedDeltaTime would change and so would your jump height.

I know everyone always says "All physics stuff must be done in FixedUpdate", but i think since your goal is to make a "one shot" event based on Input, go ahead and:

  • Move "MakeJump" inside Update() instead of FixedUpdate() so it only happens once per touch

  • Remove the fixedDeltaTime from the force calculation

  • Change the outer jump condition to something like if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) so that MakeJump only happens when the touch starts instead of happening every Update() as long as 1 finger is touching the screen.

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 ricardohenrique996 · Jan 20, 2016 at 07:16 PM 0
Share

Nose$$anonymous$$ills when I port to other platform like iOS, PC or WebGL I need make any change ? or this solution works on every platform (different framerate)?

avatar image NoseKills ricardohenrique996 · Jan 20, 2016 at 08:28 PM 0
Share

I haven't tested anything but in principle everything I suggested should apply to all platforms. You'll need to readjust at least jumpPower because these changes affect how much force gets applied in total, but after tweaking the jump to a good height in editor, i believe the jump should stay the same on any platform.

I might have missed something but in general: make sure 1 touch causes exactly 1 call to Rigidbody2D.AddForce() and that should do it. the physics engine should work the same way on any platform so the problem you have happens because now the force depends (at least) on framerate and how long you press.

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

Bullet in editor does not behave the same in build. 1 Answer

Need to Include a file in the build and use it in the runtime 0 Answers

I am trying to switch between scenes but it is not redirecting . Can anyone help with this? 0 Answers

UnityEngine.MovieTexure is required access, Help? 1 Answer

detect Gameobject which is created from c# 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