First, apologize for my poor english.
Rigidbody was added to capsule and cube, and the cube was frozen the position and rotation. This is what i expected, when I scaling this cube quickly in y direction, the capsule above it will be bounced vertically. But actually, the capsule will be pushed aside.
Scaling code here
private void FixedUpdate()
{
if (s)
{
Vector3 scaleAcc = new Vector3(2, 8, 2) - transform.localScale;
this.transform.localScale = transform.localScale + scaleAcc * 0.2f;
}
}
Currently, Setting the scale of the cube is not gonna helping. The best way to change the position and rotation of a rigidbody is adding velocity and angularvelocity. I wonder is there any way to change the scale like adding velocity and angularvelocity.
Btw there is a werid situation here (maybe bug?). When the position and rotation of the cube were frozen, and the cube goes through the ground. If the capsule touches this cube right now, the capsule will be bounced inexplicably.
Scaling an object doesn’t produce physically correct behaviors. It’s equivalent to replace the original object with a different object in a single physics frame. The physics engine will detect both bodies colliding as if they were just instanced in that situation, and will try to separate them. Some times the result will be the expected, sometimes not.
To my knowledge, there’s not a solution in Unity / PhysX that simulates the changes in scale as the objects physically growing or shrinking, so these kind of side effects will be produced.
Thanks for your reply!!! Your answer inspired me a lot. Later I learned that Unity’s rigid body is actually an encapsulation of PhysX, but changing the scale of the object is replacing it really beyond my expectation. I think I’ll find the solution about my question. Again, thanks for your answer~
Just to add to Edy’s answer, same can be said of any transform manipulation: setting the position, rotation or scale of a transform is seen by the physics engine as an instant change. So if you set the position of the transform, you’re essentially teleporting the object. If you set the scale, you’re instantly replacing the object with a bigger one. As far as physics are concerned, the object never “moved” or “grew” in any way.
As to why the capsule is pushed to the side instead of upwards in your case, it’s because that’s the shortest “escape route” from inside the box. Most physics engines use what’s known as the minimum translation vector (aka MTV) to solve collisions. In your case, moving the capsule upwards is the longest route, but the engine will always choose the shortest one.