- Home /
Unable to get OnTriggerEnter to work.[JS]
This is my first game I've tried to make so bear with me.
My goal is to call OnCollisionEnter when two objects collide: my player ship(which it might be worth mentioning is a three part object cointained under a parent object) and an asteroid.
My script_player method goes:
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "Asteroid") {
print("Collision!");
}
}
Both my player and asteroids have rigid bodies attached and isTrigger is true. Both of the settings are identical with the exception of the ''Mesh'' field where the asteroids have a mesh instead of ''None''. Also note that the rigid body has been applied to both the parent and the children. I've also added colliders to each piece.
These are the settings for EVERY piece. They are identical except for what is contained in the ''Mesh'' section.
So finally, the problem is that OnCollisionEnter will not call! I have it in a separate script with objects set up the same way. The only difference is the object itself. It is a bullet which is simply a scaled down sphere.
Here is that code:
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "Asteroid") {
var otherObject = other.GetComponent(script_asteroid);
if (otherObject.health <= 0) {
Explosion();
AddPoints(otherObject.points);
otherObject.Spawn();
}
if (otherObject.health > 0) {
otherObject.health--;
}
Destroy(gameObject);
}
}
This code works fine when the bullet hits the asteroids, but I can't get the same function when one hits the ship.
I've tried adding colliders and rigid bodies to every part of the ship, copy/pasting code(minus a few bits) but I can't figure out what I'm doing wrong. I've also looked through many other answers to questions similar to this. I'm hoping I'm missing something simple.
Thanks for any help.
Answer by Linus · Feb 22, 2014 at 07:45 PM
I think the problem is with 2 mesh colliers with rigid body.
See the table at the bottom of this page.
http://docs.unity3d.com/Documentation/Components/class-MeshCollider.html
Either use sphere collider, or another basic collider on one object, or check the convex box on the collider component.
Another possibility is that they are not aligned on one axis, this can often happen if you are making a 2D game in 3D.
up vote for making detailed question, and showing that thought went into making the best question possible. Even though the question has been asked a million times, and solution can be found rather easy on the internet.
Ended up using a basic collider. That was the solution. Thank you for the suggestion.
Answer by haim96 · Feb 22, 2014 at 07:38 PM
did you remembered to set the correct TAG for the ship? keep in mind that tags are case-sensitive.