Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Caolan · Jan 05, 2015 at 11:49 AM · c#collisioncollision detectionscriptingbasics

Detecting Collision between Charctor controller and Box Collider

Hi, I am trying to detect a collision between my player (The character controller) and a wall object (the box collider). My code looks like this:

 void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.tag == "This")
         {
             Debug.Log ("Player Hit the Wall");
         }

The script is placed on the player, and the wall is tagged "This". Anyone have ideas why nothing is showing up in the console when the player hits the wall?

Any help is much appreciated, I'm sure the answer is painfully obvious.

Thank you.

Comment
Add comment · Show 7
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 Mmmpies · Jan 05, 2015 at 02:01 PM 0
Share

The best way to see what's going on is put a Debug.Log() to show what it's colliding with before the if statement to make sure it actually is colliding first. Let us know what the results are once you've established the collision happens.

You could include the tag in the output of that Debug but I'd start off just showing some text like "Hit" to prove it works.

avatar image Caolan · Jan 05, 2015 at 02:35 PM 0
Share

You mean I should put 'Debug.Log' before the if statement above? If the debug is not within the IF statement , what would trigger it? I'm not sure I get what you mean. I am just trying to make sure the collision is detected. There is nothing else in my scene that would trigger a collision.

I have put this piece of code on a separate cube with a box collider and a rigidbody and it works fine. But I cannot figure out how to make it work on the player. I think it's something to do with the character controller?

avatar image Mmmpies · Jan 05, 2015 at 02:39 PM 0
Share

Just diagnostics, all I want to do is prove the collision happenas and rule out any issue with the tag, that's all. Copy that Debug but make it obvious which one is reporting so...

 Debug.Log("Player hit wall - debug from before if");
avatar image Caolan · Jan 05, 2015 at 02:51 PM 0
Share

I dropped that in, but no joy :(

I get the same result as before, nothing appearing in the console. it doesn't seem to be registering any collision at all, my code is currently:

 void OnCollisionEnter(Collision collision)
     {
         Debug.Log("Player hit wall - debug from before if");
 
         if(collision.gameObject.tag == "This")
         {
             Debug.Log ("Player Hit the Wall");
             
             
         }
     }
avatar image Mmmpies · Jan 05, 2015 at 02:55 PM 0
Share

O$$anonymous$$ so we can rule out the Tag. That's good & you say you've got a rigidbody on there, so let's try another common one before we resort to screen shots. Have either of the colliders got isTrigger ticked?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HarshadK · Jan 05, 2015 at 03:04 PM

You need to use OnControllerColliderHit to detect collisions for character controller.

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 Caolan · Jan 05, 2015 at 05:40 PM 0
Share

Is there no way to use OnCollisionEnter? $$anonymous$$y character does not use $$anonymous$$ove nor simple$$anonymous$$ove to get around.

I tried substituting OnControllerColliderHit into my code like so:

 void OnControllerColliderHit(ControllerColliderHit hit)
     {
         Debug.Log("Player hit wall - debug from before if");
 
         if(hit.gameObject.tag == "This")
         {
 
             Debug.Log ("Player Hit the Wall");
         }
 }

But still no result, nothing appears in the console.

avatar image HarshadK · Jan 06, 2015 at 05:24 AM 0
Share

Can you post your project files as a zip?

avatar image
0

Answer by Caolan · Jan 08, 2015 at 01:50 PM

Sorry guys I figured out a work around that I'm going to use. I can't detect collision between a character controller and box collider using OnCollisionEnter as HarshadK stated above, although I can't seem to get OnCharacterColliderHit to work either. (possibly because I am not using a Move function in my player's movement code)

So what I have done instead, and it seems to be working so far, is attaching a box collider to my player aswell as a character controller. I also added a rigidbody to my wall as well as it's own box collider. After doing this my original code seems to work just fine.

It's not an ideal solution, and I'm sure there are a million better ways to do what I am doing but sure look, as long as it works right?

Thanks so much for helping me find an answer chaps, appreciate it.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I detect world particle collisions in Unity4.0? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

help to understand collision detection 1 Answer

How to learn c sharp fast? 0 Answers

How to make collision check if other collider has component 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