- Home /
Player can jump on non ground hazards?
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. If the player tagged "Player" hits this collider, they activate the Is Trigger and spawn 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 can actually keep jumping. 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! Its like the order of operation is messed up or something. I wanted to create a second invisible collider just above the first "Hazard" one that will stop the player from using the jump command. Any help on how I can do this? Here's what I got so far, thanks everyone!`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NoJump : MonoBehaviour
{
public Rigidbody2D rb;
private ObjectMove player;
public bool jump;
public float jumpheight;
private bool onGround;
void Start()
{
player = FindObjectOfType<ObjectMove>();
}
void Update()
{
if (jump)
{
if (onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight);
}
jump = false;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
jump = false;
}
}
}`
Answer by SponOriginal · Aug 15, 2017 at 09:11 PM
When player jumps on the hazard zone, you can make the "hazard zone" disable the jumping script so player wouldn't be able to jump, I think it would be easier than putting everything in one script. For example :
void OnTriggerEnter2D(colider2D other) { GetComponent( *name of your jumping script*).enabled = false; }
Okay that sounds awesome, can you help me add it, I keep getting compiler errors. Im trying to add that to my "Hazard" Script which is what is attached to my enemies. This is the code I currently have that kills the player OnTrigger. $$anonymous$$y jumping script is called "Touch".
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
player.transform.position = start.position;
}
}
Your answer
Follow this Question
Related Questions
Player can jump on non ground items 1 Answer
Grounded check not working on sloped ground. 1 Answer
How to only Jump on Ground Items 1 Answer