- Home /
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 :)
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
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.
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.
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
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                