I have 2 game objects, an enemy and an axe. My main character throw the axe(which is a rigitbody with a boxcollider) towards an enemy(which has a box collider as well). I’m adding the colliders manually via c# script for the enemy(Transform) and the axe (rigidbody). However the axe just goes straight through the enemy instead of triggering the colliders(I have isTrigger = true). Here is my script.
Here I spawn the enemy and add a box collider(EnemyController.cs)
void SpawnTargets()
{
float x = Random.Range(-3.0f, 3.0f);
Vector3 pos = new Vector3(x, 1f, Random.Range(15.0f,23.0f));
Transform badGuy = (Transform)Instantiate(enemyPrefab, pos, Quaternion.identity);
//create a box collider and add it to the enemy
BoxCollider bc = badGuy.gameObject.AddComponent<BoxCollider>();
bc.center = new Vector3(0,1.2f,0);
bc.size = new Vector3(1.2f,3,1);
//Physics.IgnoreCollision(badGuy.collider, playerTrans.collider);
enemies.Add(badGuy);
}
Here is my Shooting.cs scripts(throws the axe)
void Shoot ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
//rotate axe so that it's facing forward
var rotation = Quaternion.Euler(new Vector3(0,90,90));
//create weapon and fire it
Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, rotation) as Rigidbody;
bulletInstance.AddForce((firePosition.forward) * bulletSpeed);
//add collider
BoxCollider bc = bulletInstance.gameObject.AddComponent<BoxCollider>();
bc.center = new Vector3(0.4391576f,0.2372868f,-1.625949e-07f);
bc.size = new Vector3(1.890458f,0.6338217f,0.159247f);
bc.isTrigger = true;
//rotate axe
bulletInstance.AddTorque(180,0,0);
//destroy ball after 7 seconds
Destroy(bulletInstance.gameObject,7);
}
}