- Home /
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));
}
}
}
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.
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)?
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.