Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SYRSA · Mar 17, 2014 at 12:49 AM · collisioninstantiateparticles

Instantiating a prefab facing away from it's collision point?

I have seen this being done with Raycast in mind, but is it possible with OnCollisionEnter, ContactPoint?

Like if I have an asteroid crashing into a planet, it spawns a prefab with a mushroom cloud particle system that has to be facing away from the surface of the planet. How will I go about instantiating the prefab with it's Y-axis facing away from the surface it collided with?

This is the script that I have made for this purpose, but it doesn't have the facing-away business to it:

 var explosion: GameObject;
 
 function OnCollisionEnter(collision : Collision) {
         for (var contact : ContactPoint in collision.contacts)
     {
         if(contact.otherCollider.gameObject.name.Contains("Rock")){
             Instantiate(explosion, contact.point, Quaternion.identity);
             Destroy(gameObject);
 
 }
 }
 }
Comment
Add comment · Show 1
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 SYRSA · Mar 17, 2014 at 03:37 AM 0
Share

Shameless self bump.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 17, 2014 at 04:21 AM

Assuming you build your model so that the part of the model you want to point away from the surface is the 'up' of that model, you can do this:

 if(contact.otherCollider.gameObject.name.Contains("Rock")){
      var q = Quaternion.FromToRotation(Vector3.up, contact.normal);
      Instantiate(explosion, contact.point, q);
      Destroy(gameObject);
 }
Comment
Add comment · Show 5 · 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 SYRSA · Mar 17, 2014 at 04:49 AM 0
Share

Well, the planets that the asteroid crashes into are spherical and has sphere colliders. $$anonymous$$aking the prefab pointing away from the center is what I'm trying to achieve.

avatar image robertbu · Mar 17, 2014 at 05:31 AM 0
Share

It is hard for me to give exact code with more context. Is this code on the planet? If so, you can get your normal vector like:

 var normal = contact.point - transform.position;

Then the same code using this normal:

  var q = Quaternion.FromToRotation(Vector3.up, normal);
  Instantiate(explosion, contact.point, q);

Again this code is assu$$anonymous$$g that you've built your model that you instantiate so that the 'up' of the model wants to point along the normal.

avatar image SYRSA · Mar 17, 2014 at 05:46 AM 0
Share

The script that spawns the explosion prefab is on the asteroid which is going to hit the planet. And the explosion prefab will be instantiated pointing away from the planet. You could easily assume this because the script has "Destroy(gameObject)" in it, since it's not the planet that gets destroyed, what would the mushroom cloud be good for then?

avatar image robertbu · Mar 17, 2014 at 05:53 AM 1
Share

@SYRSA - you are probably right. What lead me astray is that the name of the other object is 'Rock', which seemed like a name for the asteroid rather than the planet. So if what you say is true, then the normal could be calculated like:

  var normal = contact.point - contact.otherCollider.transform.position;
avatar image SYRSA · Mar 17, 2014 at 06:29 AM 0
Share

Thanks, Rob. You sure are nice helping around. The script works beautifully now.

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

20 People are following this question.

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

Related Questions

Projectile Collision help 2 Answers

Shuriken OnParticledCollision 2 Answers

Detect when 2 particle systems collide with each other? 0 Answers

Network.Destroy and collision; destroying correct object. 1 Answer

Unity 4.1 - Shuriken colide with GameObject 1 Answer


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