- Home /
Player can jump on enemy and not die
So im making a 2D platformer game and like most, there are enemies. My problem is my code for the enemies seems fine, the player touches them in any way, he dies and respawns at the beginning of the level. My issue is, this only is working when I test it on the computer in unity. When I Build and test the game on my android, if you spam the jump button, the player will just jump on enemies as soon as he touches them. If you dont spam the jump button and actually land or walk into the enemy, the player will die and respawn at the beginning of the level like he should. Its only when you are mashing the jump button i created for the touch screen (when you test it on the computer, you use the space bar to jump). This leads me to believe it must be something with my jump button on the touch screen. I cant quite figure out what it is so ill upload some related content to the issue and hopefully I can get some assistance. Thanks everyone!
//This code is attached to the player
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;
void Update()
{
//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 touchscreen button
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpheight);
jump = false;
}
//This code also involves the touch button
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Touch : MonoBehaviour
{
private Player player;
void Start()
public void Jump()
{
player.jump = true;
}
//This code is on a Controls script
if (Input.GetKey(KeyCode.Space))
{
if (onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight);
}
}
if (jump)
{
if (onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight);
}
jump = false;
}