Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Resolved Creating and destroing physics joints during runtime.

Discussion in 'Physics for ECS' started by l_Lobsternator, Jan 13, 2021.

  1. l_Lobsternator

    l_Lobsternator

    Joined:
    Aug 31, 2019
    Posts:
    12
    Recently I've been working on a simulation where I need to be able to dynamically create and destroy joints between multiple objects during runtime. I've looked around and I've tried to find a way to create joints in this way. However, the only thing I've come across is the physics examples by unity, which use an authoring component and the GameObjectConversionSystem, which as far as I can tell, only runs at the start of the simulation, and has to at that as that is the only time when the DstEntityManager is available, which is used by the EndJointConversionSystem in the CreateJointEntities method. So I'm a little bit stumped, I'm not really sure how else to create the joints other than what is shown in those examples. Any sort of help or advice would be greatly appreciated.

    I'm using unity version 2020.2.1f1.
     
    Iron-Warrior likes this.
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Basically, in order to create a joint you need to create an Entity with PhysicsConstrainedBodyPair and PhysicsJoint components, so it can be done at runtime. PhysicsJoint.Create*() and SceneCreationSystem<T>.CreateJoint() can help with that. You can find some examples in UnityPhysicsSamples\Assets\Demos\4. Joints\Scripts\RagdollDemo.cs, RagdollDemoSystem.CreateRagdoll() .

    Side note about joint lifecycle: If you delete a body Entity that takes part in a joint, you should make sure to delete the joint Entity as well (it won't happen automatically).

    This should help you get started, but please let me know if you need any further help down the line.
     
    l_Lobsternator and Sab_Rango like this.
  3. l_Lobsternator

    l_Lobsternator

    Joined:
    Aug 31, 2019
    Posts:
    12
    Ohhh, okay thank you that makes a lot more sense.