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 NoUJoe · Mar 07, 2014 at 04:13 AM · hit detectionexecution-order

OnControllerColliderHit execution order?

So I'm relatively new to Unity. New-ish to c# but not new to coding in general. So from what I've read and tested, OnControllerColliderHit will only execute when a move is performed. So if you needed to use it when you are not moving, you would just call move with 0's. I also wanted to test whether OnControllerColliderHit would get called before or after Update as I couldn't find anything online about it. From my testing, OnControllerColliderHit gets called before Update does. But this is where I get confused, I call a Move method only in Update and I debug.Log "1" in Update. In OnControllerColliderHit I debug.log "2". In my scene I have a capsule with my test script attached and I have it placed perfectly on top of a surface. When I check the console, the first ever log is "2" which really doesn't make sense because when I thought about it, OnControllerColliderHit requires Move to be called. I only had Move in Update which means that Update would have needed to run once to call Move, this would have then logged "1" first. Not "2". Maybe I'm missing something? Responses would be appreciated.

Also, here's my (very very small amount of) code if its easier to understand than my explanation:

 // Update is called once per frame
 void Update () 
 
 {
 
       controller.Move (new Vector3 (0,-0.1f,0f));
 
       Debug.Log (1);
 }
 
 void OnControllerColliderHit ()
 {
     Debug.Log ("2");
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 07, 2014 at 05:09 AM

Do this:

 Debug.Log("0");
 controller.Move (new Vector3 (0,-0.1f,0f));
 Debug.Log("1");

What you will find is that the OnControllerColliderHit() callback occurs during the Move() call. That makes sense. While I've never seen the CharacterController code, I've always assumed it does it work by Raycasting (certainly behaves that way). So the Raycast detects an object during Move() and the Move() code makes the callback to the OnControllerColliderHit().

Comment
Add comment · Show 3 · 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 NoUJoe · Mar 07, 2014 at 05:54 PM 0
Share

That does make sense about OnControllerColliderHit () being called within $$anonymous$$ove (). At first I questioned how $$anonymous$$ove () would know that OnControllerColliderHit() is defined within your class. Then I discovered reflection and after some brief reading, it makes sense that that is how $$anonymous$$ove() would detect that.

But my next question is: What if you call $$anonymous$$ove() within OnControllerColliderHit ()? Surely that would cause and endless loop?

avatar image robertbu · Mar 07, 2014 at 06:07 PM 0
Share

I expect it would hang (or more likely stack overflow) if there was no resolution to the $$anonymous$$ove(). If you kept trying different directions you might get away with it since (hopefully) there would be a resolution/base case. I tend to be pragmatic with Unity and am always trying things out to see how Unity behaves. The behaviors you'd expect are not always the ones you get. For example this question here. The only problem with this approach is that I could end up depending on undocumented behavior that could change in a new version (or situation). In practice I've never had that happen yet.

avatar image NoUJoe · Mar 08, 2014 at 03:21 AM 0
Share

I have tested it by placing a move () in OnControllerColliderHit () and it doesnt seem to endlessly cycle. Honestly I have no idea what stack overflow is, I will research this myself so you dont't have to explain it to me!! Going back to what you said about unitys character controller using raycasts. I have also thought this but have also thought it could be done with collider hit detections and positioning them on top of each other. But as I've said, I'm pretty new to unity and haven't fully looked through all the classes etc yet

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

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

OnEnable called before Awake 2 Answers

HitStop problems 0 Answers

Hit detection with unity character controler 0 Answers

Executing a script after another 2 Answers

Collision based bug 3d 0 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