Yggdrasill
Yggdrasill

Reputation: 196

Unity FixedJoint2D Bug

We are trying to implement a 2D sandbox game where the player can build his own "machine".

So what we do in code is, that we transform the new object right next to the block it shall be attached to and add a FixedJoint2D (respectiveley WheelJoint2D) to fix it. At some point we noticed though, a more complex machine (with many parts) can become unstable, when too much force is applied to it.

A record of the bug in slow motion: https://www.youtube.com/watch?v=Ym4LsuqMNfE

The angularVelocity of every part of the machine starts to become unstable at some point, before the "explosion" in the video happens. At Second 9 and 24 every object of the machine and every object it got in contact with (strangley even kinematic objects) is at the center of the world (right behind the red block) at 0,0.

Does anyone know what might cause this? It would also help when someone could tell us what the best practices are to trace such issues.

Edit: A simplified version of the code. This is how our Update() method looks like:

if (Input.GetMouseButtonDown(1) && ...) { connectSelectedBlock(); }

while connectSelectedBlock() looks simplified like this:

connectSelectedBlock(){ GameObject newBlock = Instantiate(blockToPlace); ... FixedJoint2D joint = gameObject.AddComponent<FixedJoint2D>(); joint.connectedBody = newBlock.GetComponent<Rigidbody2D>(); joint.autoConfigureConnectedAnchor = false; return joint; }

Upvotes: 2

Views: 402

Answers (1)

Pino De Francesco
Pino De Francesco

Reputation: 756

One of the most expensive and time consuming activity in Unity is to balance physics objects using joints (and the like). The video shows a typical behavior that is the result of imbalanced physics interaction. Quite often you will find that you have to put a script on each "key" object so that you can change the joint parameters at runtime. Not an easy task, I can guarantee that. Also, make sure that the physics related logic happens in the FixedUpdate() method, avoiding carefully in the Update() any operation that might have a Physics interaction of any sort.

Upvotes: 1

Related Questions