3
votes

I am trying to determine when the player is grounded or not, and using the desktop player, the method described below works perfectly, but when compiled to Android behaves rather weirdly, I am not sure if it is just delayed or what, but the bool it is updating is rarely what it is supposed to be.

The code used to determine if the player is grounded:

void OnTriggerEnter2D(Collider2D other) {
    if (other.tag == "NormalGround") {
        player.Grounded = true;
    }
}

void OnTriggerExit2D(Collider2D other){
    if (other.tag == "NormalGround") {
        player.Grounded = false;
    }
}

On desktop this works perfectly, as demo'd here by writing the grounded var to a text box on Update():

https://www.youtube.com/watch?v=CV0h18dz1cg

But on Android, it is almost as if the colliders are lagging:

https://www.youtube.com/watch?v=c66uw2_NhrQ

You can see at the end of the video that the var only changes after I have made the character jump... (the true false text is updated every frame so it's not updated on Jump()

This is how the colliders are set up:

The hierarchy:

The hierachy

The GO I am using for groundcheck:

Groundcheck

How the platforms/ ground is set up:Platforms

1: The trigger used to detect when the player is grounded (visible in inspector)

2: The colliders used to stop the player falling through the sprites (not in inspector)

The Jump() code:

    public void Jump ()
{
    if (!(PlayerDoubleJumped) && (Grounded) || ((!(Grounded) && (!PlayerJumped)))) {
        rb.velocity = new Vector2 (rb.velocity.x, JumpForce);
        animator.Play ("JumpCycle");
        PlayerJumped = true;
    } else if (!(Grounded) && (!(PlayerDoubleJumped)) && (PlayerJumped)) {
        rb.velocity = new Vector2 (rb.velocity.x, DoubleJumpForce);
        animator.Play ("JumpCycle");
        PlayerDoubleJumped = true;
    }
}

This is how I am moving the player:

this.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, 0)); rb.velocity = new Vector2 (RunningSpeed, rb.velocity.y);

Physics2D settings (default):

Physics 2d

I am using Unity 5.5.0

Does anyone have any idea what the problem is here?

1
This is interesting. This usually happens when you mess with the Physics Settings. I can't tell since the project is not in front of me. How are you moving the player? How are you jumping? Can you post the moving and jumping code? - Programmer
My guess would be that the physics interval is too large in your settings. - Farhan
@Programmer Will add that to the question now - Tiaan
@Farhan Everything is default as far as global physics settings are concerned... - Tiaan

1 Answers

0
votes

Possible fixes:

  • If you're using a mesh collider, switch to using a box collider. This works from time to time when mesh colliders are acting sketchy.

  • Use OnTriggerStay instead of OnTriggerEnter.