x


Collision problem in C#

So basically I want to be able to detect if my capsule collides with my sphere. Right now it just isn't working. Here is the script I place on the sphere:

public GameObject ParticlePrefab;

void OnTriggerEnter(Collider col){
    Debug.Log("Hit");
    Destroy(col.gameObject);
    Instantiate(ParticlePrefab, transform.position, transform.rotation);
}

For some reason it never does any of this when the capsule collides with it. I have the capsule as a Rigidbody and a trigger, so I can't think of a reason why this won't work. Any help would be appreciated.

more ▼

asked Jul 25 '11 at 08:18 PM

Infamous911 gravatar image

Infamous911
0 7 8 9

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Ok the problem was that I never actually applied the rigidbody properly. Thanks for the help!

more ▼

answered Jul 26 '11 at 07:43 PM

Infamous911 gravatar image

Infamous911
0 7 8 9

(comments are locked)
10|3000 characters needed characters left

Is the Sphere that this script is on also a trigger?

edit: actually the example code for C# does have some stuff not present in your script.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void OnTriggerEnter(Collider other) {
        Destroy(other.gameObject);
    }
}

I'm not a C# user myself but it might just need those first few extra lines and also the "public class example" part before it will work at all.

more ▼

answered Jul 25 '11 at 08:49 PM

Giometric gravatar image

Giometric
1 1

I think as long as one of them is a trigger, OnTriggerEnter() gets called on both.

Jul 25 '11 at 08:53 PM Alec Slayden

Yeah, I remembered that right after posting hehe, quickly edited with a better answer :P

Jul 25 '11 at 08:55 PM Giometric

Yes I have that, I just edited out all of the other stuff so that you could see the script that was associated with the problem.

Jul 25 '11 at 09:41 PM Infamous911
(comments are locked)
10|3000 characters needed characters left

No but I tried that and it doesn't change anything.

more ▼

answered Jul 25 '11 at 08:52 PM

Infamous911 gravatar image

Infamous911
0 7 8 9

(comments are locked)
10|3000 characters needed characters left

Ok I went ahead and replicated the scene you described, and it seems to be working fine. This is what the entire script I have looks like:

using UnityEngine;
using System.Collections;

public class test2 : MonoBehaviour {

    public GameObject ParticlePrefab;

    void OnTriggerEnter(Collider col){
       Debug.Log("Hit");
       Destroy(col.gameObject);
       //Instantiate(ParticlePrefab, transform.position, transform.rotation);
    }
}

Though as you can see I commented out the Instantiate part, the Debug.Log part is definitely working. So I'm not sure what's going on with yours.. my scene is using a simple Unity sphere and capsule, the sphere has the script on it, and the capsule has a rigidbody applied and nothing else. Are you using a character controller on your capsule?

more ▼

answered Jul 25 '11 at 09:53 PM

Giometric gravatar image

Giometric
1 1

Can you show me how you're applying the Rigidbody? Maybe that has something to do with it.

Jul 25 '11 at 10:23 PM Infamous911
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4371
x2584
x1765
x1018
x356

asked: Jul 25 '11 at 08:18 PM

Seen: 3523 times

Last Updated: May 29 at 11:27 AM