- Home /
Why will this collision check not fire?
I have 2 capsules. I control one and the other is AI controlled. I am trying to check if the player or AI capsules collide with each other. They are both rigid body's.
Here is the code which is attached to mt player capsule.
using UnityEngine;
using System.Collections;
public class Particles : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "Player" && collision.relativeVelocity.magnitude > 0.1)
{
Debug.Log("Collision detected");
}
}
// Update is called once per frame
void Update()
{
}
}
Answer by ThePunisher · Oct 30, 2012 at 11:54 PM
Ok, here's the possible problems I can see.
You didn't attach the script to either object.
The check you have in that method is failing, meaning that neither of your objects have their tags set to whatever it is you are checking (ie Enemy, Player)
One of your objects is missing a Collider component.
You enabled the "isTrigger" property on your colliders. This calls OnTrigger methods instead (http://docs.unity3d.com/Documentation/ScriptReference/Collider-isTrigger.html).
Let us know which one of these was causing your problem.
I would remove the check for the velocity until you can get the colliders to trigger.
Answer by Montraydavis · Oct 31, 2012 at 06:31 AM
I bet it has to do with this line here. I see it happen very often, and I even do it sometimes.
collision.gameObject.tag
needs to be
collision.collider.tag
for one.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083114im_/https://answers.unity.com/themes/thub/images/avi.jpg)