Search Unity

Question How to modify PhysicsJoint Constraint during runtime?

Discussion in 'Physics for ECS' started by hauwing, Mar 7, 2021.

  1. hauwing

    hauwing

    Joined:
    Dec 13, 2017
    Posts:
    69
    Hi,

    I have a system trying to modify the constraint (min, max, frequency, damping) of some PhysicsJoint (currently testing limited distance and limited DOF). I can see that from the entity debugger the values in the constraints are updated but they are not actually taking any effect at all. May I know are there any example codes showing how to do it correctly? thanks.
     
  2. davidus2010

    davidus2010

    Joined:
    Dec 4, 2017
    Posts:
    72
    Hi there, i was experiencing a similar issue when i updated the values of a limited hinge joint during runtime.
    However, as soon as the entity was moving, the joint also worked again. Another thing that also worked on stationary entites was to make only smaller, incremental changes to the constraint.
     
  3. hauwing

    hauwing

    Joined:
    Dec 13, 2017
    Posts:
    69
    Did a further test on limited hinge joint and I can actually get it to work with the following simplified codes, except for the situation where the rigidbody (physicbody) is in "sleep" mode, which I need to manually move it a little bit to activate it to make this system work. However, I still cannot get limited distance and limited DOF to work.



    Code (CSharp):
    1.         Entities.ForEach(
    2.             (
    3.                 Entity _entity,
    4.                 ref PhysicsJoint PJ, // limited hinge joint
    5.                 ref comp_data data) =>
    6.             {
    7.  
    8.                     var constraints = PJ.GetConstraints();
    9.  
    10.                     var c0 = constraints[0];
    11.                     c0.Min = data.cur_angle;
    12.                     c0.Max = data.cur_angle;
    13.                     constraints[0] = c0;
    14.                     PJ.SetConstraints(constraints);
    15.  
    16.             }).ScheduleParallel();
     
  4. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    The <Tests/JointTest/Modify Tests> test scene is changing the Joints. The associated script is here.