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 /
  • Help Room /
avatar image
0
Question by icecub · Mar 28, 2016 at 06:37 AM · collision

Detect collision between player and ground

I'm still learning Unity and right now I'm trying to make my player able to jump. Of course I don't want my player to be able to jump on forever, so my idea was to only enable jumping when the player is in contact with a floor object. This is the code I have so far:

 public class PlayerController : NetworkBehaviour
 {
 
     public float speed;             // Player movement speed
     private bool grounded = true;   // Contact with floor
 
     private Rigidbody rb;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     // Show a different color for local player to recognise its character
     public override void OnStartLocalPlayer()
     {
         GetComponent<MeshRenderer>().material.color = Color.red;
     }
 
     // Detect collision with floor
     void OnCollisionEnter(Collision hit)
     {
         if (hit.gameObject.tag == "Ground")
         {
             grounded = true;
         }
     }
 
     // Detect collision exit with floor
     void OnCollisionExit(Collision hit)
     {
         if (hit.gameObject.tag == "Ground")
         {
             grounded = false;
         }
     }
 
     void FixedUpdate()
     {
         // Make sure only local player can control the character
         if (!isLocalPlayer)
             return;
 
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce(movement * speed);
 
         // Detect space key press and allow jump if collision with ground is true
         if (Input.GetKey("space") && grounded == true)
         {
             rb.AddForce(new Vector3(0, 1.0f, 0), ForceMode.Impulse);
         }
     }
 }

But it seems OnCollisionEnter and OnCollisionExit never trigger. So the player is still able to jump whenever he wants. Am I doing something wrong?

Edit: After some debugging it seems both OnCollisionEnter and OnCollisionExit work perfectly fine. It's the if statements returning false. I checked if the Ground object actually exists, just to be sure with:

 if (GameObject.Find("Ground") != null){ Debug.Log("ground found"); }

But that returned true just as expected. So there's something else going on.

Edit 2: Strangely enough both these seem to return "Untagged":

 Debug.Log(hit.gameObject.tag);
 Debug.Log(hit.collider.tag);
Comment
Add comment · Show 2
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 Guhanesh · Mar 28, 2016 at 06:41 AM 0
Share

Why are you using Fixed Update to get inputs? Use Update()

avatar image icecub Guhanesh · Mar 28, 2016 at 06:42 AM 0
Share

As I said, I'm still learning. And this is what's inside Unity's first official tutorial. So I expected it to be correct that way. Anyway, I've done what you advised. It doesn't solve my issue though :)

1 Reply

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

Answer by Veerababu.g · Mar 28, 2016 at 07:24 AM

make sure about the tag once.I mean the tag what you write is same as what you given in inspector

Comment
Add comment · Show 2 · 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 icecub · Mar 28, 2016 at 07:30 AM 0
Share

I did. The tag is the same and it also exists. Ins$$anonymous$$d I've tried to debug it by letting it tell me which tag is being returned on collision. But all I get is "Untagged". And I'm 100% it is tagged, haha.

avatar image icecub · Mar 28, 2016 at 07:39 AM 0
Share

You were partly right actually! It turned out I was confusing the objects name with the objects tag! So the object really didn't have a tag at all. I've added the tag and it works perfectly fine!

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

62 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

Related Questions

Object sticks to the side of the collider 1 Answer

Can I detect a collision between two triggers on other objects 1 Answer

CharacterController and Instantiating an object with collider 1 Answer

Help with Destory a vechicle code. 1 Answer

How do I count Hits ?? 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