• 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
Question by lPr0c · Jul 29, 2017 at 12:08 AM · c#rigidbodyraycastplayercolliders

How do I use a sphere collider to tell if my player is on the ground?

So I have made a script that moves my player character. There are a few things that I want to add, but I can't figure them out by myself sadly. I have gone to around 16 different pages and have yet to find what I'm looking for. What I need help with is detecting when my player is standing on an object. I have a Boolean called IsGrounded and I need it to send false when I am in the air.

I thought I came up with a solid solution to the problem which would work. And it did! But... it's very glitchy, almost to the point where it's nonfunctional.

The script looks like this:

 using UnityEngine;
 using System.Collections;
  
 public class PlayerColliderDetection : MonoBehaviour
 {
     public Player_Move ThePlayer;
 
     void OnTriggerEnter(){
         if (ThePlayer){ThePlayer.IsGrounded = true;}
     }
     void OnTriggerExit(){
         if (ThePlayer){ThePlayer.IsGrounded = false;}
     }
     void OnTriggerStay(){
         if (ThePlayer){ThePlayer.IsGrounded = true;}
     }
 }

Regarding my issue (I'm not very comfortable showing my scripts), my movement script does this:

     public GameObject BlankObject; // A blank object to apply a collider to.
         private GameObject GroundedCollider; // The object that will be set as the collider.
         void Start()
         {
             // Creats a collider.
             GroundedCollider = Instantiate(BlankObject, Player.transform);
             GroundedCollider.AddComponent<SphereCollider>();
             GroundedCollider.GetComponent<SphereCollider>().radius = 0.4f;
             GroundedCollider.GetComponent<SphereCollider>().isTrigger = true;
             GroundedCollider.AddComponent<PlayerColliderDetection>();
             GroundedCollider.GetComponent<PlayerColliderDetection>().ThePlayer = Player.GetComponent<Player_Move>();
          }
          void Update()
     {
     
     RaycastHit hit;
             if (Physics.Raycast(Player.transform.position, Vector3.down, out hit, 5000.0f))
             {
                 HitY = hit.transform.position.y;
             }
     
             GroundedCollider.transform.position = new Vector3(GroundedCollider.transform.position.x, HitY, GroundedCollider.transform.position.z);
     }
     
  

The idea is the collider follows the players x and z, uses a raycast pointing down from under the player, the position is retrieved, and the y is used. This would create a functional Vector3 every update for the collider, allowing it to follow. And the collider does follow the player. But, it bounces from the players feet, to the floor, or wherever they are standing on. It teleports on the y-axis... and it changes the IsGrounded Boolean constantly, even when I'm jumping. It's frustrating and I would really appreciate some help!

Thankyou for anything,

Justin

Comment

People who like this

0 Show 0
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
Best Answer

Answer by lPr0c · Jul 29, 2017 at 12:52 AM

Never mind, I fixed it. My player character was stopping the raycast at the edge of the collider, teleporting it from the ground to the player, etc. In the end, I set my Players layer to be unaffected by the raycast, and now it works! But thank you for helping!

Comment

People who like this

0 Show 0 · 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

Answer by SteenPetersen · Jul 29, 2017 at 12:19 AM

I would simply set a collider on the feet of the player in a new child gameObject of the player that sends information to the player about collisions. Put this collider its own layer, and in Edit=>Project Settings=>Physics/physics2D set it to collide ONLY with the ground layer.

the script would look something like this:

 public class GroundChecker: MonoBehaviour {
 
     public MyPlayerScript thisPlayer;  // can be made private later, but just to check in the editor that you have got ahold of the script.
 
     private void Start()
     {
         thisPlayer= transform.root.GetComponent<MyPlayerScript >();
     }
 
     private void OnTriggerEnter2D(Collider2D other)  // assuming its a 2D game with 2D colliders
     {
             if (other.tag == "ground")
             {
                 thisPlayer.Grounded= true;
             }
         }
 
     private void OnTriggerStay2D(Collider2D other)
     {
             if (other.tag == "ground")
             {
                 thisPlayer.Grounded= true;
             }
         }
 
     private void OnTriggerExit2D(Collider2D other)
     {
             if (other.tag == "ground")
             {
                 thisPlayer.Grounded= false;
             }
         }
 
     }
 
 }

hope that helps.

Comment

People who like this

0 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 lPr0c · Jul 29, 2017 at 12:38 AM 0
Share

That doesn't exactly solve my problem. The problem I am having is not that I can't tell if the player is on the ground or not, it is simply that the collider that the player runs into when he hits the ground is unpredictably teleporting from my feet to the HitY from my feet to the HitY. But either way thank you for that.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

378 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 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 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 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 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 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 rotation restricted even though I don't want it to be 0 Answers

Gravity and rotation control 0 Answers

parent/child object teleportation issues 0 Answers

How to make "enemy" have a frustum(cone) view and not a circle radius 2 Answers

Raycast doesnt detect object in front of rigidbody (player is stuck on wall) 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