- Home /
Colliding with the same object thousands of times per second
Using this script,
void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody body = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
if(hit.gameObject.tag == "CubeEnemy") {
healthHandler.AdjustCurrentHealth(-10);
Debug.Log("Hit Enemy");
}
}
Was hoping for it to be something singular, like OnCollisionEnter, any ideas?
Comment
It's called only once when a character controller uses $$anonymous$$ove and hits an object. Generally you call $$anonymous$$ove once per frame though.
Answer by Anxo · Jan 15, 2013 at 01:25 AM
void OnControllerColliderHit(ControllerColliderHit hit) {
if(!checking){
checking = true;
Rigidbody body = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
if(hit.gameObject.tag == "CubeEnemy") {
healthHandler.AdjustCurrentHealth(-10);
Debug.Log("Hit Enemy");
}
checking = false;
}
}
Oh if you have a first person controller, just use the "$$anonymous$$ove" command to move him around. It will respect collisions.
Ins$$anonymous$$d of using transform.position or transform.translate.
Your answer