- Home /
Can't get OnTriggerEnter to work
Hi, once again I find myself confused as hell by collisions and triggers. Right so what I am currently doing is just creating simple raycast-fueled AI that avoids scenery and another kind that attacks the peaceful ones.I'm not having trouble with the raycasting part, but getting the OnTriggerEnter to work has eluded me. Here is the code for the OnTrigger:
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag ("Player")) {
Debug.Log ("trigger enter");
other.renderer.material.color = Color.yellow;
Destroy(other.gameObject);
}
}
At this point I am not even recieving the Debug.Log so can only assume there is a problem with my collider/rigidbody setup. The above code is on the bot that kills peaceful bots. The 'killer' bot has a kinematic rigidbody and a capsule collider that isn't set to trigger. The peaceful bot just has a capsule collider with trigger enabled.
Any help would be massively appreciated. I can't particularly see why you'd need more of my code to help, but if there is anything in particular that would help, please let me know.
Thanks a lot!
Something needs a rigidbody, probably.
Look at the chart at the bottom of this page: http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html.
$$anonymous$$inematic rigidbodies DON'T COUNT as rigidbodies.
well, according to that chart, everything I have tried should work - for example a collision between two kinematic rigidbodies with triggers should work, but it doesn't. So I'm even more stumped now considering that's what I had thought might be wrong with it.
okay so I have changed the scripts around so that the OnTriggerEnter is now on the peaceful bots that have their trigger collider enabled. here is the whole script: using UnityEngine; using System.Collections;
public class capsule : $$anonymous$$onoBehaviour {
public Transform Capsule;
public Transform baddies;
public int capSpeed = 2;
RaycastHit hitInfo;
//float distance = 50.00f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Capsule.transform.Translate (0, 0, 1 * capSpeed * Time.deltaTime);
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Debug.DrawRay (transform.position, fwd * 6, Color.green);
//if (Physics.Raycast(transform.position, fwd, 5)){
if(Physics.Raycast(transform.position, fwd, out hitInfo, 6)){
if(hitInfo.collider.CompareTag("wall")){
Capsule.transform.Translate(0, 0, 0 * Time.deltaTime);
if(Capsule.transform.rotation.y < 20){
float randomY = Random.Range(-90, 90);
Capsule.Rotate (0, randomY, 0 * Time.deltaTime);
}
else if(Capsule.transform.rotation.y > -20){
float randomY = Random.Range (-90, 90);
Capsule.Rotate(0, randomY, 0 * Time.deltaTime);
}
}
else if(hitInfo.collider.CompareTag("baddies")){
Capsule.transform.Translate(0, 0, 0 * Time.deltaTime);
Capsule.Rotate(0, 180, 0 * Time.deltaTime);
}
}
}
void OnTriggerEnter(Collider other){
Debug.Log ("trigger enter");
if (other.gameObject.CompareTag("baddies")) {
Debug.Log ("trigger enter");
this.renderer.material.color = Color.yellow;
Destroy(this.gameObject);
}
}
}
And I've also attached these pictures of the inspector so you can see the collider setups.