Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by andrewwebber25 · Sep 14, 2017 at 04:52 PM · jumpingtouch controlstouchscreengroundedground detection

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrewwebber25 · Sep 14, 2017 at 05:51 PM 0
Share

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

capture.jpg (104.6 kB)
avatar image rewin123 andrewwebber25 · Sep 14, 2017 at 07:22 PM 0
Share

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;
      }
avatar image andrewwebber25 rewin123 · Sep 14, 2017 at 08:32 PM 0
Share

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

avatar image MT369MT · Sep 15, 2017 at 06:21 PM 0
Share

Use OnCollisionExit2D (Collider2D Col) { if (Col.gameObject.tag == "ground" { player.grounded = false; } }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Player does not jump when running at the edge of a ledge (How to add jump tolerance?),Player Does not jump when at the edge of a ledge 1 Answer

How to only Jump on Ground Items 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges