OnCollisionEnter Question.

Hello Unity Community!

I am using OnCollisionEnter to take the enemy’s health when I shoot an arrow and it hits the enemy. The character has a Sphere Collider as trigger which is used as the enemies detection. It also has a character Controller to keep it from going through the ground, and it has a box collider for collision detection from the arrow.

Problem is, when I shoot the arrow even if it doesnt hit it reduces the enemy’s health. From my testing, it seems that even if it enters into the enemies sphere collider marked as trigger, it still takes damage. If I shoot completely away from the enemy, it doesnt do damage. But if its at all near the enemy or what I believe to be inside that sphere collider, it does damage.

How can I fix this? Does OnCollisionEnter detect As Trigger colliders or just standard colliders? Maybe there is a better way to make my enemy detect the player other than using a large sphere collider marked as trigger?

Oh also - my arrow does have a rigidbody on it.

Here is the applicable code for this:

var hit : GameObject;
var arrowDamage : float = 10.0;
var enemyIsHit : boolean = false;

function Start()
	hit = GameObject.FindWithTag("Enemy");

function Update()
	if(enemyIsHit == true)
	hit.collider.SendMessageUpwards("ApplyDamage", arrowDamage, SendMessageOptions.DontRequireReceiver);
	enemyIsHit = false;	

function OnCollisionEnter()
enemyIsHit = true;

Check this out: http://answers.unity3d.com/questions/339297/detect-explosion-character-controller.html

Ok I think you have to ‘hack this’. What I would try is to:

  1. Add an empty Game object as child of my character 2.
  2. Add a box collider and set it to trigger and add a rigidbody (disable this)
  3. Put a script in there which handles OnTriggerEnter
  4. In this method, that is on trigger, disable the character controller on the parent and enable the character controller. `

Please let me know if this helps.