- Home /
OnTriggerEnter not recognising collision ?(Solved)
I have two objects, a cannonball and a target. For some reason (probably something minor that I'm just not seeing) OnTriggerEnter just isn't recognising when the two collide.
The target has a box collider with 'is trigger' checked. The cannonball has a ridigbody and sphere collider.
This is the script -
using UnityEngine;
using System.Collections;
public class MegonHealth : MonoBehaviour
{
public static int megonLife = 4;
public Transform bossExplosion;
void OnTriggerEnter(Collider collider)
{
if(collider.gameObject.name == "CannonBall")
{
megonLife -= 1;
Instantiate(bossExplosion, transform.position, transform.rotation); // Instantiate Explosion Effect
}
}
}
What am I missing guys ???
Answer by psykojello2 · Feb 26, 2015 at 06:52 PM
Try to give both your objects a rigidbody. You can check "isKinematic" on the rigidbody if you don't want it to be affected by physics.
I'm assuming both objects have a collider as well.
See if either object is on a particular layer. If they are, then check Project Settings->Physics and make sure that the two layers are able to collide in the Collision Matrix.
Hope that helps.
Oh, also put a debug.log statement inside your onTriggerEnter function before you check for cannonball. Something like "Debug.Log("Collided with "+other.gameobject.name");" just to check if you're colliding with anything at all.
Edit: Solution: Check the game object's tag instead of name. Set a tag for your cannon ball called "CannonBall" and check it in your OnTriggerEnter function. if(collider.gameObject.tag == "CannonBall") DoStuff()
If you're working on a 2D game, be sure to use OnTriggerEnter2D, Collider2D and RigidBody2D
ok I edited the OnTriggerEnter event to do a debug test -
void OnTriggerEnter(Collider collider)
{
Debug.Log("Collision Detected");
/*
if(collider.gameObject.name == "CannonBall")
{
megonLife -= 1;
Instantiate(bossExplosion, transform.position, transform.rotation); // Instantiate Explosion Effect
}
*/
}
Getting a message every time, so the trigger is obviously recognising the collision.
The spelling and the capitalisation of the CannonBall are exact.
Both the trigger object and the other object are on the same layer.
???
Add the name of the collided object to see what it's looking at:
Debug.Log("Collision Detected "+collider.gameObject.name);
get this message -
Collision Detected CannonBall(Clone) UnityEngine.Debug:Log(Object) $$anonymous$$egonHealth:OnTriggerEnter(Collider) (at Assets/$$anonymous$$egonHealth.cs:13)
There's your answer right there :) The object's name is CannonBall(Clone) not CannonBall. This is happening because you're instantiating the CannonBall in your scene.
Ins$$anonymous$$d of checking for the gameObject's name, you should check for its tag. Set a tag on your cannonBall prefab called "CannonBall".
Then check your collision for :
if(collider.gameObject.tag == "CannonBall")
{
//your logic here. Boom!
}
To add a tag, click the drop down under the GameObject's name in the inspector and choose "Add Tag..." and create a new one. Don't forgot to set the tag after creating it. Good luck!
Answer by MathieuBarbier · Feb 26, 2015 at 05:18 PM
Did you check frame by frame that your colliders actually collide. Sometimes if your object is moving very fast it can miss the collision.
Answer by X10KND · Feb 26, 2015 at 05:40 PM
i'm not sure but i guess your actual gameobject name did not match with the on in the script (script line 13). please reply if the problem is solved
Your answer
Follow this Question
Related Questions
Collision Checking 1 Answer
Adding Force to a Rigidbody through OnTriggerEnter 1 Answer
Trigger when 3 triggers collide. 1 Answer
Collision Detection without a RigidBody 2 Answers