How do I prevent stacked objects from being spoiled by collisions?
I have objects falling from different positions. And they form a stacked tower. When the tower is extended, it may hit objects that we missed from other positions. In this case, the objects in the tower should not move and the objects hit by the tower should move. I've tried a lot of code found at the bottom as comment lines. However, different problems arise in all of them. The tower formed by the objects that make the desired collisions must be able to remain stable. How can I do what I want?
@dadaygames, to me it's not clear in your description what you want exactly. Can you provide diagrams and/or video of the behavior as it is now (that you don't want) and the behavior you do want?
I tried to explain by drawing I hope it was. $$anonymous$$y goal is to push the objects hit by the tower from the side when the character moves. And the tower remains stable.
Answer by streeetwalker · Sep 16, 2020 at 02:22 PM
If I understand your diagram, I think what is happening now essentially how physical objects behave in the real world - is that your sense?
What you want is for the tower to impervious to all collisions?
If you have a Rigidbody and a collider attached to an object, it will react with equal and opposite force to a collision where the forces have been applied through the physics engine. This is called collision resolution. If you remove or disable the Rigidbody component, the collision resolution forces will not be calculated on the object.
So I believe that is what you need to do as each piece settles on the tower, disable or remove its Rigidbody component.
That may present other problems. For example, if you are using the physics system to add forces to move the whole tower, you'll need to find another way of doing that.
When the rigidbody disappears, the objects do not stay on top of each other, but begin to intertwine.
then you must have something else going on.
I just ran a test on cubes that drop on top of each other, and destroying the Rigidbody does not alter their position in anyway - they remain frozen in the position they are in at the time the Rigidbody is removed. So we have to see what else is going on in your code.
Here is my sample code:
public class TestRemovRB : $$anonymous$$onoBehaviour {
public GameObject[] cubesRB;
void On$$anonymous$$ouseDown() {
for( int i = 0; i < cubesRB.Length; i++ ) {
Destroy( cubesRB[i].GetComponent<Rigidbody>() );
}
}
}
So how are you removing/disabling the Rigidbody?
That's all the code that affects the physics of objects. I destroy rigidbody this way. What kind of error could there be in these codes?
these codes are inside the 3 types of spawn objects.
And the objects are intertwined in this way. Could there be a problem with the physics settings in another section?
I have experience with this recently. What I discovered was this:
At the time OnCollisionEnter runs, the collision resolution forces have been calculated, but the collision cannot be completely considered completely resolved, in sense, until the objects come to a rest. It may take many FixedUpdate frames for that to happen, depending on how hard objects hit each other.
When two objects hit, they may penetrate each other if they are moving fast enough. I think in your case what a happens is they are penetrating at the time OnColllisionEnter is called, and you are removing their Rigidbodies at that time, freezing them in those positions.
In my case, the easy solution was to wait for enough FixedUpdate frames for the objects to move to their resting positions, and then destroy their Rigidbodies. But this is not really a neat solution.
I think a better, more perfect solution is for you to calculate where their resting positions should be, destroy the Rigidbody, and then use their transforms to position them in their perfect resting positions that you calculated.
How can I calculate resting positions. $$anonymous$$y objects with 3 different y sizes are co$$anonymous$$g up randomly. I don't know if I fully understand what you said, but as far as I understand it doesn't seem possible.
Answer by twotostudio · Sep 16, 2020 at 04:49 PM
I did continious instead of discrete in collision detection in rigidbody settings. And I guess they don't intertwine anymore.