How should an object maintain inertia after breaking away from an 'anchor' (picture a yoyo with a string snapping)

The best way I can describe the idea is in this way:

You have a person using a yoyo.
While they are using the yoyo they throw it in an ‘around-the-world’ maneuver in which the person throwing the yoyo makes the yoyo spin around the person’s hand in a big circle (with the string still fully extended). Now picture someone cutting the string during the spin, the yoyo would fly off into the direction it was traveling without the bound of the string.

I want to simulate the motion of the yoyo object as it breaks away from the string.

In my current project, I have one object moving in circles around another object (there isn’t a string as in the yoyo example, but the yoyo allows for an easier picture), I want to suddenly be able to break the ‘string’ connecting the two objects and allow the object that would be the yoyo to fly freely in the direction it was traveling as it broke loose.

How should I approach this situation?

#Good news, It’s incredible easy!

This is the sort of thing where you discover the “wonders of Unity” and modern nVidia-style physics.

Literally all you do is detach the object from the parent group. That’s it.

Often it is literally one line of code: essentially…

yoyo.parent = null;

(Just to be clear, it’s the “transform” component that literally holds the parent-chid relationships, so typically your actual javascript will be along the lines yoyoBody.transform.parent = null)

The only other tip is, very simply you sometimes also fool around with eg. the colliders and other components during such a separation. An example: you want the hatch to bash off a spaceship and exhibit the independent behaviour you describe. Typically, while the hatch is on the ship, you’ll have the collider of the hatch simply disabled. (The only reason being it would pointlessly interact with the main collider of the spaceship.) So, when you let the hatch “go free” (perhaps during a hard spin for example), you would do the one line of code above, and also, you would turn on that disabled collider of the hatch. (Conceivably, you might wait a couple of frames until the hatch is clear of the ship body.) Similarly you might have say a script (perhaps for a flashing light or sound effects) attached to the hatch and/or the other body - you may well need to turn those scripts on or off, as relevant. (Again this is only detail, fundamentally it’s just the one line of code above.)

To workshop this, make two unit spheres, give them each a sphere-collider and a sensible rigidbody, make one the child of the other, connect them with a hinge and drop them on to a floor - they will bounce around satisfyingly. Now add a button using unityGui which triggers a script. The script simply dissolves the parent relationship using the one line of code above. You’ll see they fling apart perfectly!!!

Hope this is what you were describing!

Once again, this is one of these things that is surprisingly “unbelievably easy” in Unity - refreshing. I’ve always found in Unity when you figure something like this out, it takes a day or two to absorb that life can be that easy :slight_smile: It goes against Protestant Work Ethic, you know?

Further: note that a common approach when you do something like, say, a character dying or being exploded or radically changing state: What people often do is, actually “at that instant” you sneakily replace your motorbike model/scripts/system with an entirely different motorbike model/scripts/system. Your first motorbike would be a normal connected model with suspension, or whatever. But the new “after explosion” model would actually be perhaps four or so different bits, in fact with wild velocities on then flying outwards, making exploding sounds, flame effects, etc etc. (Note that your art department can just go wild creating that “motorbike on the verge of explosion system” as they wish complete with scripts, audio, etc etc - as a self contained package.) Again this seems a bit “strange” until you do it, but what you do when the motorbike hits the wall or whatever, you very simply SWAP OUT (two lines of code) the real motorbike for “motorbike about to explode” – just eliminate the real motorbike and instantiate the prefab of the about-to-explode motorbike … that’s it! Incredibly, in that example, it’s literally just two lines of code to get rid of the “normal” model, and instantiate the “just about to explode” model - again, literally 2 lines of code!

(PS you may know all this already – there is a very wide range of experience on this board (to put it mildly!) so this may help someone else.)

For the record personally I DO NOT LIKE the “swap out to a new prefab” approach. Really the only reason to do it is because it “makes programming easier” - you can imagine during an explosion you have to change 3 or 4 variables and relationships, etc - the “swap to a new prefab” trick avoids that. I have no problem changing variables so I usually just do that sort of thing “manually”.

I mention all this because: someone may well suggest to you that the way to do what you describe is just swap out to a new prefab (composed of “bits” rather than one whole system). In fact, in this case, that would be the wrong approach … it would be quite tricky as you’d have to completely pointlessly copy over all the physics of the old system. (Note that with an “explosion” swapper-prefab, the small parts, typically, simply have wild velocities - that looks just great for an explosion. You, on the other hand, are doing a REAL physics simulation of a separation, which is cool, and as I say, surprisingly “even” easier!)

The correct and “amazingly miraculous” solution for what you describe is, happily, nothing more than separating the object from it’s parent. (You may also have to be tidy regarding any minor changes needed to perhaps colliders, etc - however it is often literally as simple as just setting the transform.parent=null and Unity does the rest, the physics behaviour “emerges” properly.)

Hope it helps

PS! A whole other factor here is, in some similar situations you might be using hinges in Unity. (They are just a type of joint.) IMO hinges are not the way to proceed with what you describe, but it is a related concept. Incredibly, hinges actually “do everything for you” by, for example, breaking on their own once the stresses are too high, and then, on their own, they cut the parent-child relationship for you, making the part which “fell off” become a new independent object. (In the editor, you can actually SEE it flip up to another position in your object list at the very moment Unity makes it a separate object!) So in short, as you continue to investigate this type of physics, you may also wish to play with hinges. However, you should first be expert in “letting go” of a part of an object (hatch, yoyo, whatever) as described above.

Note this is an absolutely standard part of almost any game, that you will have to use constantly in any normal game programming. parent=null. Hope it helps.