- Home /
OnControllerColliderHit won't trigger if not moving
Hi, I have a wall that moves and if it hits you you are supposed to die, the problem is, that is only triggered if the character controller is moving, so if you stay still, the wall goes throught you and you don't die, is there anyway way to fix this issue? By the way the wall has a rigidbody attached to it
Thanks in advance.
Edit: Adding some code by request, only the important part of it
void OnControllerColliderHit (ControllerColliderHit hit) {
if (hit.transform.tag == "PowerUp"){
PowerUpScript powerUpScript = hit.transform.GetComponent<PowerUpScript>(); ......... not showing all this part since it doesn't matter
}
else if (hit.transform.tag == "NeonWall") {
ApplyDamage();
}
}
void ApplyDamage () {
Destroy(gameObject);
}
edit 2: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnControllerColliderHit.html
"OnControllerColliderHit is called when the controller hits a collider while performing a Move." From my understanding, it will only be called when the character controller is moving, is that right? is there a workaroud? I really need one.
maybe its the physics time detection. This is set in the edit -> physics settings
Added the important part of the code, what is the setting I'm supposed to change inside physics settings?
Answer by Owen-Reynolds · Apr 08, 2014 at 01:38 PM
Use a standard OnCollisionEnter (I think on the wall, or on the player.)
During the physics step, then player counts as a regular stationary collider. So it can be hit by rigidbodies, and will give/get OnCollisionEnter based on their motion. As you note, OnCharConHit is only triggered when the player moves. The whole charController thing is sort of a hack:
The normal physics system moves everything together, in the "physics step", outside of all scripts. That's where it calls OnCollisionEnter/Exit. But CharControllers skip physics (to move more like a computer game player.) They move during the script. As far as the physics system is concerned, they magically teleport when it isn't looking. So they won't fire an event moving into non-rigidbodies, maybe things that are asleep... . OnCharConHit is a fix, to see all colliders, and happens during the move command (I think.)
Thanks, using OnCollisionEnter worked perfectly, but it only worked on the wall by the way
Answer by Joyner · Apr 08, 2014 at 02:57 PM
yes because you are programing "OnControllerColliderHit" that means when this controller hit something not when something hit it, so, you have to use: OnCollisionEnter(GameObject c) {
if(c.tag == "Something"){ // c have to have a rigidbody
doSomething....
}
}