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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Physics Joint Example/Sample

Discussion in 'Physics for ECS' started by jakenicolaides, Dec 23, 2022.

  1. jakenicolaides


    Sep 28, 2022
    Can anyone provide me an example of how to implement a Physics Joint, specifically a FixedJoint between two entities? I've created the physics joint using:

    Code (CSharp):
    1. PhysicsJoint.CreateFixed();
    But I have no idea what I'm supposed to do with the joint? Where do I apply it? Apologies in advance if this is a really stupid question.

    I did look at the RagDoll example but they use a function called CreateJoint() which I can't seem to find so I don't know if the function name has changed or I've missed something.
  2. theman224


    Mar 23, 2017
    This is what i do for a joint between entityA and entityB and works for me. I use a new separate entity to hold the joint because i have multiple joints on the same rigidbodies. If there is only one joint you can place the joint on entityA or entityB

    Also you could ask chatGPT for more examples (although it only knows about older versions of ECS)

    EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

    RigidTransform rigidTransformY = new RigidTransform();
    rigidTransformY.pos.z = 20f;

    Entity entityJointHoldery = entityManager.CreateEntity();
    entityManager.AddSharedComponentManaged(entityJointHoldery, new PhysicsWorldIndex(0));

    bodyPair = new PhysicsConstrainedBodyPair(entityA, entityB, false);
    entityManager.AddComponentData(entityJointHoldery, bodyPair);

    joint = PhysicsJoint.CreateFixed(rigidTransformY, new RigidTransform());


    entityManager.AddComponentData(entityJointHoldery, joint);