Reputation: 69
I am trying to use Physics2D in Unity and simply place blocks on top of each other. My project setup:
I have a sprite as floor with a Box2D collider. I have a prefab which is also a sprite with 1.5 x 0.5 x 1 scale and also a box collider and a rigidbody2D.
What I do is to place blocks on top of each other on the same x axis: Here is my code:
public GameObject testInstance; //Prefab Box
public float m_fSpawnY = -3.1f; //Start Y for first box
public float m_fSpawnYAdd = 0.5f; //for next box
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.N))
{
Instantiate(testInstance, new Vector3(0, m_fSpawnY , 0), Quaternion.identity);
m_fSpawnY += m_fSpawnYAdd ;
}
}
My problem now is that the more boxes I add, the more the objects below get "squeezed", which might be correct for real physics but for my use-case I would like to disable the deformation completely because the build tower gets instable the more I place on top.
Is it somehow possible to deactivate deformation? I tried to use Physics Material 2D but it only gives friction and bouncyness but I have set boucyness to 0 and it has no effect.
Thanks and best regards
Upvotes: 2
Views: 455
Reputation: 468
You can fix this problem by changing some value in your project settings, basically the "velocity iterations" and the "position iterations". This will consume more resources but will generate a more accurate move and therefore less "bouncing". You should play around with those value to find what suit the best for your project.
I think this is a side effect of the "sample rate" at which the physics is calculated, leading to small overlap of the object and then getting rejected creating this bouncy effect. the more your "sample rate" is high the more the collision detection is accurate but the more ressources are used.
Upvotes: 2