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 homer_3 · Jun 25, 2020 at 05:23 PM · collisionphysicscharactercontrollercollision detection

OnCollisionEnter not working with CharacterController

So I have this script on 2 objects

 void OnCollisionEnter(Collision collision)
 {
     Debug.Log(gameObject.name + " OnCollisionEnter Entered");
 }

Object 1 is a sphere with a sphere collider, rigidbody and this script. I put it over a plane with just a box collider. Run the game, the sphere falls on the plane and the log appears. Great.

Object 2 is a character model with a rigidbody set to kinematic, character controller, this script, and another script that calls Move() so the character will fall down onto the plane. When the character falls down to hit the plane, nothing is logged. If I use OnControllerColliderHit, the log appears, but it's being logged every frame. I also tried adding another collider to the model (not a trigger), but i still don't get the log. Is the issue that Move() won't allow OnCollisionEnter to be called? Is there a better way to do this?

Comment
Add comment · Show 1
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 ShadoX · Jun 26, 2020 at 04:03 PM 0
Share

Have you had a look at https://answers.unity.com/questions/723010/how-to-make-kinematic-rigidbody-react-to-some-coll.html ?

Also, if you have a look at https://docs.unity3d.com/ScriptReference/Rigidbody.OnCollisionEnter.html it states:

 Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.

$$anonymous$$eaning that you either have to adjust your Rigidbody or find a different way of getting it to work, if you absolutely must have it kinematic ^^"

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by mbro514 · Jul 04, 2020 at 10:20 PM

Character Controllers don't call OnCollisionEnter or OnCollisionStay when they collide with other GameObjects. They only call OnControllerColliderHit(). Unfortunately, OnControllerColliderHit() is called every single frame that the Character Controller is touching the other object, and there is no similar method for only the first frame that a collision occurs. You would have to code this function yourself.

Comment
Add comment · Show 4 · 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 harranj · Aug 16, 2020 at 06:10 PM 0
Share

Hey I've been having this problem. How could you code this yourself? Could you have a cube or cylinder or something at the same position with oncollisionenter? Thanks

avatar image mbro514 harranj · Aug 19, 2020 at 05:00 PM 0
Share

Hey @harranj. I'm pretty sure it would not be a good idea to use a cube or a cylinder with a kinematic rigidbody, as it would only be able to send out collision callbacks if it hit a dynamic rigidbody, which is probably not the behaviour you're looking for.

How I would code my earlier answer would be (I've never tried it, but it should hopefully work.)

 bool isTouchingGround;
 
 void OnControllerColliderHit(ControllerColliderHit hit)
 {
 if (hit.gameObject.layer == Layer$$anonymous$$ask.NameToLayer("Ground"))
 {
 if (isTouchingGround)
 {
 //  Put in the code that you would have put into OnCollisionEnter
 }
 else
 {
 // Put in the code that you would have put into OnCollisionEnter
 isTouchingGround = true;
 }
 }
 }

void FixedUpdate() { // To replicate OnCollisionExit, put some code in here (probably using a Physics Query) to tell if you are touching the ground, and if you're not... if (isTouchingGround){ // Put in the code that you would have put into OnCollisionExit isTouchingGround = false; } }

You could use code similar to this for any other collision callbacks that you need.

avatar image Edy mbro514 · Aug 20, 2020 at 10:00 AM 0
Share

Just a side note, collisions between kinematic bodies may be enabled in the Physics section of the Project Settings: Set Contact Pairs $$anonymous$$ode to Enable All Contact Pairs.

avatar image SubicaBubica · Feb 20, 2021 at 11:55 AM 0
Share

Worked for me, this is the only solution I think. Here is part of my script. I used collision with tags.

     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if (hit.gameObject.tag == ("Wall"))
         {
             Debug.Log("work");
         }
     }

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

256 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

Related Questions

Is there a rigidbody character controller 3rd person that can handel stairs (steps)? 1 Answer

Can't use Rigidbody or Character Controller 0 Answers

Physic based golf game - ball bouncing off the connection of colliders on flat surface 2 Answers

How can I make two Character Controllers Collide 2 Answers

Alternatives to CharacterController.Move to drive a car 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