Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed May 01, 2018 at 10:48 PM by strausse1.
avatar image
0
Question by strausse1 · Apr 26, 2018 at 07:03 PM · velocityinstantiate prefabdestroygameobject

taking velocity of object a to apply to an instantiated shattered object.

hi have a spell to launch an earthplate, as soon as it collides with something it gets destroyedand a cellfractured prefab spawns , however the prefab has no velocity and this just looks weird on collision.

super simple script: public class EPhealth : MonoBehaviour {

 public GameObject destroyedVersion;
 
 
 void OnCollisionEnter(Collision collision)
 {

     Instantiate(destroyedVersion, transform.position, transform.rotation);
     Destroy(gameObject);
     Debug.Log("Hit");
     
 }
    

}

i have also noticed that the prefab gets instantiated a bit higher it seems like so its hopping for a splitsecond. thanks for the help :)

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Eno-Khaon · Apr 27, 2018 at 05:29 AM

You might want to take a look at Collision.impulse and Collision.relativeVelocity for some ideas. Without knowing all the physics attributes of your objects, it's hard to guess what the ideal approach is, but either of these should get you a usable piece of information for this.

With that in mind, how would you implement something using these? Well, here's a starting point (even if it's not a final solution):

 void OnCollisionEnter(Collision collision)
 {
     // new
     Vector3 impactForce = collision.relativeVelocity;
     
     GameObject newObj = Instantiate(destroyedVersion, transform.position, transform.rotation);
     Destroy(gameObject);
     Debug.Log("Hit");
     
     // new
     float variationScale = 0.5f; // Arbitrary value for simple example
     float impactForceMag = impactForce.magnitude;
     foreach(Transform child in newObj.transform)
     {
         Rigidbody rb = child.GetComponent<Rigidbody>();
         if(rb != null)
         {
             rb.AddForce(impactForce + (Random.insideUnitSphere * variationScale * impactForceMag), ForceMode.VelocityChange);
         }
     }
 }

Edit: Updated to include child objects

Comment
Add comment · Show 3 · 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 strausse1 · Apr 27, 2018 at 07:12 PM 0
Share

hi first of all thanks for your effort. so for further information i instantiate a prefab at mouse position (a simple plate floating in the air rb=kinematic) as soon as i click it gets a transform.looktat/turns kinematic to false at my mouse position and force impact in the facing direction to shoot it however on collisionenter im using this script to shatter it. i made a shattered version in blender and all the fractures have their own rigidbody so its gonna be hard to reference that. my main goal is to get the velocitiy of the mainobject transfered to all the fractured parts to no just fall down but shatter in the direction the object got destroyed. i hope i made my point kind of clear i started with all that unity gamemaking a few days ago and i dont fully understand everything yet. maybe i can reference the child rigidbodys in some way in the last line? or maybe i just take the shattered versions and set kinematic on all rigidbodys to false on collision enter i think that could give me a smoother effect ins$$anonymous$$d of instantiating a new object.

avatar image Eno-Khaon strausse1 · Apr 27, 2018 at 08:58 PM 0
Share

Thanks for the input. I updated my answer to include applying the force to children of the base "shattered" object, with a little bit of variation so they're not all stuck together.

avatar image strausse1 · Apr 28, 2018 at 12:10 AM 0
Share

thanks so much!! i got it working now my only problem now is that they kinda get pushed in the opposit way but i think i can fix that. you are a hero! thanks

Follow this Question

Answers Answers and Comments

80 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 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 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 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

Re-instantiated prefabs desist changes in text component 1 Answer

Can't addforce to instantiated object 0 Answers

Destroy() not working on prefab instance 2 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Having problem with instantiating and destroying prefabs 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