• 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
1
Question by Vis-Studios · Nov 14, 2013 at 07:50 PM · 2djumpisgrounded

Ground Detection

I'm trying to detect ground, on both platforms and the main ground, in my 2d game. So, What I've done is given all of my platforms and my main ground the tag "ground". However, I can't seem to get my IsGrounded() method to work. Can someone help me out?..

 bool IsGrounded()
     {
         foreach(GameObject groudnObj in GameObject.FindGameObjectsWithTag("ground")){
             if(Vector3.Distance(collider2D.transform.position, groudnObj.collider2D.transform.position) < 2f){
                 if(groudnObj.collider2D.transform.position.y < this.collider2D.transform.position.y){
                     return true;
                 }
             }
         }
         return 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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by prof · Nov 14, 2013 at 10:17 PM

For ground detection i'll recommend use linecast http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.Linecast.html

Or you may attach empty gameobject with trigger collider to your character and use it as ground detector.

Here's example project https://www.assetstore.unity3d.com/#/content/11228

btw, checking all objects is a bad idea)

Comment
Add comment · Show 1 · 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 Vis-Studios · Nov 15, 2013 at 05:49 AM 0
Share

Thank you! Linecast is what i was looking for :) The documentation on the new 2d system isn't very good yet.

avatar image
1

Answer by Klaus-Eiperle · Nov 14, 2013 at 10:16 PM

Hi,

  • add box colliders to the payers feet.

  • Switch on 'Is Trigger'

  • add a script like this:

       using UnityEngine;
         using System.Collections;
         
         public class DetectGroundHit : MonoBehaviour {
                 public PlayerController refPlayerController;
                 
                 void OnTriggerEnter() {
                     if (refPlayerController) {
                         refPlayerController.groundHitYN=true;
                     }
                 }
                 
                 void OnTriggerStay() {
                     if (refPlayerController) {
                         refPlayerController.groundHitYN=true;
                     }
                 }
                 
                 void OnTriggerExit() {
                     if (refPlayerController) {
                         refPlayerController.groundHitYN=false;
                     }
                 }
         }
    
    
  • the script PlayerController is applied to the Player Object. This script has a public bool which is set when the player hits a ground object.

  • add to the platforms and main ground box colliders or mesh colliders.

Is that what you need?

All the best, Klaus

Comment
Add comment · Show 1 · 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 kriptite · Jun 22, 2016 at 01:56 PM 0
Share

This worked splendidly for me, simple and agile if u put it on a distinct gameObject that u can move around, thanks man.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Itween gradual jump 2d 1 Answer

Jump and run on a circle (2D) 0 Answers

2d Beat Em Up Jump Functionality 0 Answers

Increase Jump Through Mouse Clicks? 1 Answer

Accelerating speed 2 Answers


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