- Home /
Player can jump on non ground items
Alright everyone, im desperate here and will be rewarding points if someone can help me fix this stupid issue. I made a 2D platformer in unity on my computer. The user uses the left and right arrow keys and the space bar to run and jump. Everything works fine, the player can only run and jump when they are on the ground. This means if they are already in the air or land on an enemy, they cant jump.
I use a ground check for this and it works well. I simply give the ground platforms the tag "ground", and the player can interact with them. Now i made some touch buttons on the screen so the user can play the game on the phone. The buttons seem to work and the player moves relatively well. The problem is that when you use the jump button on the touch screen, if you spam it, the player can jump on non "ground" tagged items. If you are playing the game on the phone and jump on an enemy, you will die (like you should). If you jump on that same enemy BUT you are spamming the jump button when you do it, he jumps off of them like they are a normal platform. Im assuming this means there is something wrong with my touch screen controls or ground check (this issue is only on the phone).
Im really new to this and ive been trying to fix this for like a month and nothing seems to work. Ill upload code and if you guys see anything that i should change, I will reward 10 POINTS to whoever can solve it! Thanks so much!
This is attached to my Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class Player : MonoBehaviour
{
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
public bool jump;//this was added
public float jumpheight;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
public bool moveright;
public bool moveleft;
void Start()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
//used to stop finger swipes
// This code is used to move the player with keyboard
anim.SetBool("Grounded", grounded);
anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
if (Input.GetAxis("Horizontal") < -0.1)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetAxis("Horizontal") > 0.1)
{
transform.localScale = new Vector3(1, 1, 1);
}
//This Jump code is to jump when space bar is pressed in unity
if (Input.GetButtonDown("Jump") && grounded)
{
rb2d.AddForce(Vector2.up * jumpPower);
}
//This code is using the bools from above to make player move by touch icons
if (Input.GetKey(KeyCode.LeftArrow))
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetKey(KeyCode.RightArrow))
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
transform.localScale = new Vector3(1, 1, 1);
}
if ((jump) && grounded)//for jumping with the touchscreen button
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpheight);
jump = false;
}
if (moveright)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
transform.localScale = new Vector3(1, 1, 1);
}
if (moveleft)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
transform.localScale = new Vector3(-1, 1, 1);
}
}
These are my Touch Controls attached to my touch buttons in the Canvas
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Touch : MonoBehaviour
{
private Player player;
void Start()
{
player = FindObjectOfType<Player>();
}
public void LeftArrow()
{
player.moveright = false;
player.moveleft = true;
}
public void RightArrow()
{
player.moveright = true;
player.moveleft = false;
}
public void ReleaseLeftArrow()
{
player.moveleft = false;
}
public void ReleaseRightArrow()
{
player.moveright = false;
}
public void Jump()
{
player.jump = true;
}
}
i know this post is ridiculously long but Im seriously at a standstill and really want to keep moving forward. Let me know if you guys want any pics or more code. I still have some more ifGrounded code and the hazard code that is attached to my enemies.
Answer by MT369MT · Sep 14, 2017 at 05:22 PM
Hi Strange your bug
if ((jump) && grounded)//for jumping with the touchscreen button
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpheight);
jump = false;
Try to add here the line: grounded = false; i am not sure it help. But where is the script that handle the collisions for check if it is grounded?
I added you ground = false; and it didnt seem to make a difference.
To be honest with you I have a script called GroundCheck that is attached to the feet of my player (empty game object). I deleted it from the GroundCheck game object and my player still seems to act the same. Not having any issues with jumping unless its on the mobile device.
How could it be that if i dont have that script attached to my player or the ground check object, it still is checked as grounded?? The Player code that I originally posted (the first chunk of code) is the only thing that references being grounded through the public bool! When I play the game in unity and the player is on the ground, the "grounded" check box is checked under the Player script. When he jumps, the grounded checkbox is not checked. When he is jumping on an enemy constantly, it never says he is grounded, so why is he able to jump on them? hmm seems like there may be something up with my ground check. Here is my GroundCheck code even though it doesnt seem to matter if i have it attached to anything or not.
![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;
}
}][1]
[1]: /storage/temp/101870-capture.jpg
You havent "if" for noground colliders. He can jump using enemy collider, before interaction with enemy. Use it
void OnTriggerEnter2D(Collider2D col)
{
player.grounded = col.tag == "Ground"; //Add Ground tag to ground objects
}
void OnTriggerStay2D(Collider2D col)
{
player.grounded = col.tag == "Ground";
}
void OnTriggerExit2D(Collider2D col)
{
player.grounded = false;
}
I messed around with it a little and tried this:
void OnTriggerExit2D(Collider2D col)
{
if (tag == "Player")
{
player.grounded = false;
}
}
At most, the player just falls through the platform even when its labeled as ground and has a trigger on it. Is there a way to do this without triggers? It doesnt really make sense to me for there to be triggers in the first place
Use OnCollisionExit2D (Collider2D Col) { if (Col.gameObject.tag == "ground" { player.grounded = false; } }
Your answer
Follow this Question
Related Questions
Player can jump on non ground hazards? 1 Answer
Player can jump on enemy and not die 0 Answers
Grounded check not working on sloped ground. 1 Answer
How to only Jump on Ground Items 1 Answer