Reputation: 699
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.
[UPDATE with JhohnPoison answer]
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));
}
}
}
Upvotes: 0
Views: 77
Reputation: 1061
The bug you're experiencing is related with frame rate game is running at. Higher frame rate you have, more frequently update will be called (maximum 60fps). Therefore different amount of force can be applied to the body.
When you're working with physics you should do all job in FixedUpdate and use Time.fixedDeltaTime to scale your forces (instead of using constant "7").
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)); // here's a scale of the force
}
}
}
More info: http://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html http://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html
Upvotes: 5