- Home /
Player can jump on invisible non ground items
Alright everyone I think I got a tricky one for ya. So im making a 2D platformer where the player has a small collider attached to his feet called groundCheck. Its your basic ground check where if the player is not on the "ground", they cant keep jumping or walking. Everything seems to work fine except when I test it on my phone. In the game, if you jump to a platform and miss it, the player falls for a second and then hits an empty game object with a "Hazards" script attached to it. It the player tagged "Player" hits this collider, they spawn back to the beginning of the level to restart. Again, this works well when testing in unity but not quite when tested on the phone.
On the phone, if the player misses a platform and falls onto the hazard border, they will die (like they should) but if they keep pressing the jump icon, the player will jump one time and then when they hit the hazard border the second time, they will die. The jump button works fine in all other parts of the game, they can not continuously jump in the air or anything, its just when they are hitting the hazard colliders. Its puzzling because the border hazards have no affiliation with "ground" and should not allow the player to jump one or two times before dying! Plus its hard to test because like I said, it works fine on the computer but not the phone. Could it be my Jump touch button from my canvas? Sorry Im typing so much, I will upload anything you guys need me to. Thanks for the help!
Answer by bgprocks · Jul 31, 2017 at 07:59 PM
are you using coroutines at all, how are you processing the jump request. It sounds like you have an order of operations issue. Like your jump is getting processed before the collision code. one trick you can do is disable jumping as soon as the user is lower that the platform, or have a trigger object below the platform but above the hazard, once the player hit the trigger disable processing jump request.
I think you may be on to something, its really the only thing I can think of that makes sense. Im going to put up some bits of code.
This is everything that has to do with jumping from my Touch code (this is the code for the jump button on the canvas that the player interacts with through the touch screen):
public void Jump()
{
player.jump = true;
}
This is all the jump related code attached to my Player:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine.EventSystems; public class Player : $$anonymous$$onoBehaviour {
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
public bool jump;//this was added
public float jumpheight;
public bool grounded;
void Update() { anim.SetBool("Grounded", grounded); //This Jump code is to jump when space bar is pressed in unity if (Input.GetButtonDown("Jump") && grounded) { rb2d.AddForce(Vector2.up * jumpPower);
}
if ((jump) && grounded)//for jumping with the touch screen button
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpheight);
jump = false;
}
} void FixedUpdate() { //Fake friction / Easing the x speed of our player if (grounded) { rb2d.velocity = easeVelocity; }
And possibly most importantly, this is my GroundCheck code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
public class GroundCheck : $$anonymous$$onoBehaviour {
private Player player;
void Start()
{
player = gameObject.GetComponentInParent<Player>();
}
void OnTriggerEnter2D(Collider2D col)
{
player.grounded = true;
}
void OnTriggerStay2D(Collider2D col)
{
player.grounded = true;
}
void OnTriggerExit2D(Collider2D col)
{
player.grounded = false;
}
}
Let me know if anything stands out! :)
Nothing is jumping out at me (not a pun) but will look it over again when I reboot my brain.