- Home /
Transform collider not detecting collision on rigidbody collider
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);
}
}
Answer by j_basson · May 17, 2013 at 06:07 PM
Thanks Mexallon , however the reason why the right trigger wasn't being his was because I didn't have a script attached to the gameobject that was dynamically being generated(had to dynamically add the script to the gameboject).
Answer by Mexallon · May 12, 2013 at 07:59 PM
Hej there. If you turn "isTrigger" on true on any collider it is not physically effected by other colliders but going through the enemy. Also it does not call the events "OnCollisionEnter" though "OnTriggerEnter" - I don't know which method youre using.
Primarily I want to use OnTriggerEnter but it doesn't seem to fire it. I want the axe to trigger the event when it hits the enemy , then I can handle what happens from there on. Right now the only time OnTriggerEnter is firing is when my character gets hit by the enemy. Ins$$anonymous$$d of the axe hitting the enemy as well.
I see. You're repositioning the collider of the axe. Did you checked for the colliders to be present and in place in-game?
Edit P.S: its probably easier to use and less faulty to add the collider already to the prefab
there is a collider on the enemy and on the axe during runtime, I've checked. I turned the isTriggered off and the axe seems to hit the enemy and bounce off, but Ins$$anonymous$$d of bouncing off, I want it to fire the OnTriggerEnter but it won't when I turn the trigger on for the rigidbody collider. The only OnTriggerEntere that gets fired is when my character gets hit by the enemy
hm strange. Any Update on that case? I wonder what's the root of the problem
not really. I'm wondering whether it might not be the hierarchy setup I have that is messing with it. I have the shooting script attached to my main camera which is a child of the main character. When none of the colliders have the triggers enabled then the axe collides with the enemy but when I activate the trigger it still won't register it unless my main character gets hit by the enemy.
Your answer
Follow this Question
Related Questions
simple onCollision and onTrigger problem 1 Answer
Child calling parent's OnTriggerEnter when no Rigidbody is attached to child 1 Answer
OnTriggerEnter not working, tried everything! :( (C#) 3 Answers
Waypoint Question(Conflicting Collision) 1 Answer
CharacterController and Normal rigidbody Box Collider Collision issues 0 Answers