OnTriggerEnter function with one triggercollider and one non-triggercollider

Hi, I am new to Unity and I’m making my first game. note, I’m not new to programming. I am trying to make a tree which you can chop down with an axe. I completed all of my scripts, no problems there, but then I found out that I can just walk through the tree because of the collider being a trigger. To solve that I added an empty gameobject as a child of the tree, and added a collider.

The problem I’m having right now is that when I’m chopping down the tree, the OnTriggerEnter function runs two or three times when hitting the tree with the axe. This is my OnTriggerEnter function:

void OnTriggerEnter(Collider other) { if (other.GetComponent<WeaponController> ()) { Debug.Log ("Script found"); script = other.GetComponent<WeaponController> (); if (script.chopping == true) { health = health - script.damage; Debug.Log (health); playSound (); if (health == 0) { DestroyTree (); } } } }

I hope you can help me out here. Thanks.

Why exactly are you adding a collider to your trigger instead of just making your trigger a regular collider? The rule is simple:

  • Should be in your way = no trigger
  • Should let you through = trigger

I assume that for some reason, you got to think that you need OnTriggerEnter to handle collision events and thus need a trigger. This is not the case. If you have two colliders that are both non-triggers, just use OnCollisionEnter instead of OnTriggerEnter. Note that the optional paramter on this method supplies a lot more data about the collision event than OnTriggerEnter.