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, 2011 at 11:23 PM

Stef gravatar image

Stef
31 61 935 45

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

4 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][1], 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, 2011 at 11:51 PM

aldonaletto gravatar image

aldonaletto
56.8k 30 76 291

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

Nov 25, 2011 at 12:26 AM Stef

I know this is old, but I came across it and couldn't leave it!

It destroys everything because 'if' is unbraced so only the immediate statement following the if is conditional.

if (collision.gameObject.tag == "Ground_Smoke")
{
     Instantiate(explosionPrefab,transform.position,transform.rotation);
     Destroy (gameObject);
}
Apr 30 at 08:24 PM meat5000
(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, 2011 at 01:16 AM

littlemegzz gravatar image

littlemegzz
11 16 12 13

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

Remove semicolon(;) after if statement.

more ▼

answered May 27, 2013 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, 2013 at 10:03 AM

christinam gravatar image

christinam
746 1 4 6

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

x4000
x2712
x532

asked: Nov 24, 2011 at 11:23 PM

Seen: 11567 times

Last Updated: Apr 30 at 08:25 PM