- Home /
Two Objects - Contraction?
I have two objects. One sphere is moving left and one sphere is moving right. Now, when they're too close (when colliders touching), they should contracted and destroyed.
How can I solve this?
Answer by MakinStuffLookGood · Jan 14, 2015 at 04:12 PM
There are many ways to do this. I believe you mean "collision" as opposed to "contraction".
Simplest way would be something like:
void OnTriggerEnter (Collider col)
{
// Do some check here to make sure it's the object we want to destroy on
if (someCheck)
{
Destroy(gameObject);
}
}
We'd need more information about the exact behaviour you want.
Really you should just look up collision detection. There are lots of official resources on the Unity website. Start here: http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers
You may find it helpful to do some more intro tutorials before asking more questions on forums like this.
When the two white spheres touching each other, then they will be destroyed. When the two green (colliders) touched, then the two objects contracted and then when the two white spheres touch together, they will be destroyed.
Ahhh gotcha. So when the objects are in range, they should pull towards one another.
Take a look at OnTriggerStay. What you can do is have a force constantly applied to the objects velocity in the direction B-A (where A is the object detecting the trigger and B is the triggering object). You have that code on the larger trigger spheres, and then the destruction on collision code on the spheres themselves. Shouldn't be too complicated, but let me know if you run into any issues.