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 24, 2017 at 07:16 PM · collider2djumpinggroundgroundedground detection

How to jump on only "ground" colliders

Hey guys, so i think im having an issue with my ground check not working the way i want. The player seems to do well with not continuously jumping when in the air. My problem is that if my player interacts with any "collider", it treats it like its the ground and can jump off of them. This includes enemy AI and collectible coins. I dont really blame my script cause that seems to be what im telling it to do. How to I specify that the platforms are "ground" and the coins and enemies are not "ground"? I even made the Layer of my platforms called "Ground" but im not sure how to reference it in my script. Any ideas?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GroundCheck : MonoBehaviour {
 
     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;
     }
 }
 
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
2

Answer by Phantom_101 · Sep 24, 2017 at 07:41 PM

The above code is wrong. If the player touches anything, then the player is grounded. I don't think you would want that. What you need to do is to add a tag (e.g. "ground" or "jumpable") and try to find it when OnColliderEnter or OnColliderExit is called. I don't have a clue as to why you did -->Trigger<-- instead of -->Collider<--. Here's a sample:

 private bool isGrounded;
 void Update(){
      if(Input.GetKeyDown(KeyCode.Space)){
          if(isGrounded){
              //jump
          }
      }
  }
  
  void OnCollisionEnter(Collider other){
      if(other.tag == "ground"){
          isGrounded = true;
      }
  }
  
  void OnCollisionExit(Collider other){
      if(other.tag == "ground"){
          isGrounded = false;
      }
  }
Comment
Add comment · Show 3 · 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 24, 2017 at 07:59 PM 0
Share

You are exactly right with everything the player touches making it "grounded", no idea why they are doing that in the tutorial im following. I added your code and it has no errors. Now i took my platform and added a Tag called "ground" like you mention in the script and added it to the platform. The only problem is that the player jumps when ever the space bar is pressed. So even if im in the air, i can press the space bar and he can keep jumping. Any idea?

avatar image Phantom_101 · Jan 02, 2018 at 01:50 AM 0
Share

Oops... Think I made a mistake. For the OnCollision methods, the parameter type should be of Collision ins$$anonymous$$d of Collider. After you've changed it, be sure to use other.gameObject.tag. See if it works now.

avatar image Phantom_101 · Jan 02, 2018 at 01:56 AM 0
Share

Yep. I tested it and it worked. And then I started playing around with 2D area effecters lol. It should be fine for you now. Sorry about the late replies.

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

Grounded check not working on sloped ground. 1 Answer

Jump if ground is detected 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 set up line cast for two groundchecks? 0 Answers

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