x


Collision with tag

Lads, I'm trying to destroy an object when it hits the ground and have a little particle effect instantiate. I'm also trying to use a tag so that this only happens when the object collides with the ground and not other objects... Here's what I've got thus far:

    var explosionPrefab:Transform;

function OnCollisionEnter(collision: Collision) {

if (collision.gameObject.tag == "Ground_Smoke");

Instantiate (explosionPrefab,transform.position,transform.rotation);

Destroy (gameObject);
}

I think I'm in the ballpark, but not quite there yet. Assistance would be appreciated.

Thx,

Stef

more ▼

asked Nov 24 '11 at 11:23 PM

Stef gravatar image

Stef
31 20 21 24

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

5 answers: sort voted first

Yeah, you're very near! If you remove the ";" after the if, your code should work.
But to have better results, you must use the collision point and normal to generate position and rotation for the smoke prefab. The Collision structure has the field contacts, an array of contact points which inform also the surface normal in the hit point; in this case, you can use only the first contact point, contacts[0]:

function OnCollisionEnter(collision: Collision) {
  if (collision.gameObject.tag == "Ground_Smoke"){
    var hit = collision.contacts[0]; // let's get the first contact
    // find the rotation needed to make the smoke perpendicular to the hit surface
    var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
    Instantiate (explosionPrefab, hit.point, rot);
  }
  Destroy (gameObject); // destroy the projectile anyway
}
more ▼

answered Nov 24 '11 at 11:51 PM

aldonaletto gravatar image

aldonaletto
55.4k 21 65 261

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

Thanks ald,

Your code and mine both have the same problem. The object is destroyed when it touches any collider... not just the ground. The ground tag is called "Ground_Smoke"... is it being used correctly?

Thx,

Stef

more ▼

answered Nov 25 '11 at 12:26 AM

Stef gravatar image

Stef
31 20 21 24

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

Remove semicolon(;) after if statement.

more ▼

answered May 27 '13 at 12:00 PM

amands gravatar image

amands
1

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

Try using if(col.gameObject.CompareTag("Name of tag")) { //code here }

This worked for me when having the same problem :)

more ▼

answered Jul 31 '13 at 10:03 AM

christinam gravatar image

christinam
746 1 3 5

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

You may be able to try changing it to a trigger instead of a collision. As an example for you I have the following script (which works) in my current prototype.

function OnTriggerEnter (col : Collider) {
    if (col.tag == hitObjectsTag)
    {
        overlord.SendMessage("NoteHit", soundClipNumber);

        Instantiate (dropletPrefab, transform.position, transform.rotation);
        Destroy(this.gameObject);
    }
    if (col.tag  == backWall)
    {
        if (isNotJelly){
            // need to implement a way to change the soundClipNumber depending on the note hit
            overlord.SendMessage("NoteMissed");
            anglerFish.SendMessage("MoveBack");
        }

        Destroy(this.gameObject);
    }
}

hitObjectsTag and backWall are just variables I set as Strings so I can edit them in the inspector.

more ▼

answered Nov 25 '11 at 01:16 AM

littlemegzz gravatar image

littlemegzz
11 6 6 7

(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:

x3678
x2498
x499

asked: Nov 24 '11 at 11:23 PM

Seen: 9587 times

Last Updated: Jul 31 '13 at 10:03 AM