Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Stef · Nov 24, 2011 at 11:23 PM · collisioninstantiatetag

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
7

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
}
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stef · Nov 25, 2011 at 12:26 AM 0
Share

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

avatar image meat5000 ♦ Stef · Apr 30, 2014 at 08:24 PM 0
Share

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);
 }
avatar image
-1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by amands · May 27, 2013 at 12:00 PM

Remove semicolon(;) after if statement.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges