- Home /
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
Answer by aldonaletto · Nov 24, 2011 at 11:51 PM
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 }
Thanks ald,
Your code and $$anonymous$$e 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
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);
}
Answer by littlemegzz · Nov 25, 2011 at 01:16 AM
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.
Answer by christinam · Jul 31, 2013 at 10:03 AM
Try using if(col.gameObject.CompareTag("Name of tag")) { //code here }
This worked for me when having the same problem :)
Answer by TheFish657 · Feb 20, 2016 at 01:06 PM
The delete gameobject function is outside of the if statement, meaning that it will destroy if it hits any collider but only instantiate the particle effetc if it hits the ground.
Your answer
Follow this Question
Related Questions
Network.Destroy and collision; destroying correct object. 1 Answer
Issues with instantiated prefabs 1 Answer
How can I keep track of score without each new basketball shot reseting my points? 1 Answer
Lower Object Health If Collides With "Player" Tag,Lower Object Health If Collides With Player Tag 1 Answer
Set parent of instantiated object. 0 Answers