Problems destroying object when dropped
This is my first post so apologies for the scurviness.
So think of my gameObject as an egg. I want it to be destroyed if it hits the ground too hard, as well as break when another object is dropped on it. The code I am using works for the most part, however the gameObject only breaks when an object with velocity hits it, not when it hits an object with no velocity, i.e. the ground. Here is what I am working with (C#):
public GameObject broken;
public float force;
void OnCollisionEnter(Collision collision)
{
if (collision.relativeVelocity.magnitude > force)
{
Destroy(gameObject);
Instantiate(broken, transform.position, transform.rotation);
}
}
So does anyone know how I can make the gameObject be destroyed if a certain force is applied whether or not it is hitting something or being hit by something?
Thanks!
Answer by unity_6PeTxfqj15B_dA · Aug 19, 2018 at 12:53 AM
I'm a beginner myself but from what I see in your code, you are calculating if the relative velocity is greater than force.. But what if the relative velocity was negative? I think relative velocity is the velocity of B from A's perspective but A from B's perspective should be negative I think since the object is falling onto your "egg" What u can do is take the absolut value of your relative velocity and see if it's greatee than "force" and it might work Again I'm just assuming but it might work..
That sounded like a great idea, however after using
void OnCollisionEnter(Collision collision)
{
Debug.Log (collision.relativeVelocity.magnitude);
}
I realized that the log was still only displaying impact with other moving objects, upon impact with the floor, no value is printed, not even negative, so unfortunately I don't think $$anonymous$$athf.Abs will help in this scenario.
Your answer