- Home /
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");
}
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().
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?
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.
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
Follow this Question
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