- Home /
C# GameObject is not detecting collision with Character Controller
Hi everyone, I have a script that detects when a Character Controller collides with a gameobject. I have attached my script to the gameobject but the script isn't detecting when I move the Character Controller into the gameobject. Any idea what is wrong with my script?
using UnityEngine;
using System.Collections;
public class PlayerDetect : MonoBehaviour {
void OnControllerColliderHit(ControllerColliderHit playerCollider){
if(playerCollider.gameObject.tag == "Player"){
Debug.Log("Hit Player");
}
}
}
For OnCharacterColliderHit() to work, it must in a script attached to the game object with the character controller.
Ok but I want to put this script on to the gameobject that the Character Controller is walking into. Is there a way to do that and get it to work?
You need to put it on the game object with the character controller. Then you can filter for the objects you want to impact the character.
Answer by aldonaletto · May 12, 2013 at 01:11 AM
OnControllerColliderHit events are only sent to the CharacterController script. If you want to do something in the other object's script, use SendMessage to call some specific function in it - for instance:
CharacterController script:
void OnControllerColliderHit(ControllerColliderHit hit){
if (hit.normal.y < 0.9){ // filter out ground collisions
// try to call PlayerHit in the other object
hit.gameObject.SendMessage("PlayerHit", hit, SendMessageOptions.DontRequireReceiver);
}
}
Other object's script:
void PlayerHit(ControllerColliderHit hit){
Debug.Log("Player Hit");
}
A somewhat faster alternative is to check for the specific PlayerDetect script and, if found, directly call PlayerHit (CharacterController script):
void OnControllerColliderHit(ControllerColliderHit hit){
if (hit.normal.y < 0.9){ // filter out ground collisions
// if the other object has PlayerDetect script...
PlayerDetect otherScript = hit.gameObject.GetComponent<PlayerDetect>();
if (otherScript){ // call its function PlayerHit
otherScript.PlayerHit(hit);
}
}
}
NOTE: This event happens all the time due to collisions with the ground. In order to quickly filter out such collisions, a simple trick is to ignore collisions where the normal angle from the ground is too steep. In this example, only normal angles < 64 degrees (sin(64) = 0.9) are processed. This trick works fine because hit.normal is a normalized vector, and in this case its Y coordinate is numerically equal to the sine of the elevation angle.
Answer by CgShady · May 12, 2013 at 12:59 AM
If you want to have it on the object you run into, you can use OnCollisionEnter.
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
I switched to OnCollisionEnter but it's still not detecting the player.
using UnityEngine;
using System.Collections;
public class PlayerDetect : $$anonymous$$onoBehaviour {
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.tag == "Player"){
Debug.Log("Hit Player");
}
}
}
OnCollisionEnter events are sent to scripts attached to both, the rigidbody and the other object, but there's a problem: such events are generated only when a rigidbody hits a collider or CharacterController, not the other way around (a character hitting a rigidbody, for instance)