- Home /
Detecting Collisions with Sphere Collider
Hey Guys, I am trying to detect a collision with a sphere collider in my game, but am having a difficult time finding information on it. What I have is : A cylinder with an added "Sphere Collider" Component (added through the gui "Add component"). A block called "Unit" which is going to be colliding with the sphere collider. I am trying to make the sphere collider attached to the cylinder print a message when the block enters the collider field. I looked at the documentation and dont know what to do with the code:
public class Tower : MonoBehaviour
{
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter(Collision col) {
if(col.gameObject.name=="Unit"){
Debug.Log(col.gameObject.name);
}
}
}
Anything would help guys. Thanks,
This should work just fine as long as you make sure Units have a RigidBody component. Also if you're spawning unit prefabs make sure you name them "Unit" as instantiated prefabs usually get a (clone) suffix "Unit(clone)"
@xortrox for some reason it still isnt working. Cylinder is a rigid body, Unit is a rigid body. Cylinder has the collision sphere attached to it, but still not luck. It wont print out any statements, so I know its not detecting the collision... $$anonymous$$now any good examples? --I should also mention that I have the sphere collider set to a trigger. Thanks,
If you are using triggers, then you need to use OnTriggerEnter() ins$$anonymous$$d.
Could one of you post it as an answer and then @camtronius can approve it as an answer. That way the ForumBot won't keep pushing this thread up in the stack. :-)
Answer by camtronius · Apr 21, 2014 at 06:34 AM
@xortrox I actually solved it. I was wanting the ontriggerenter function and changing the parameters to collider! Thanks man,