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.

Best way to set Unity.Physics limited hinge min/max angle constraint?

Discussion in 'Physics for ECS' started by florianhanke, Aug 29, 2019.

  1. florianhanke


    Jun 8, 2018
    Hi all,

    I have a piece of ugly looking code that I'm unsure if it's the best way to go – perhaps one of you has a better solution.

    I have car-related physics code in which I have a spindle attached to the car's chassis, which turns on a limited hinge joint. I'm using the PhysicsJoint's min/max constraint to turn the spindle.

    So in my VehicleMechanicsSystem, what I am doing is iterate over the PhysicsJoint-s, and if they are a spindle, set the constraints:

    Code (CSharp):
    1. public void Execute(ref PhysicsJoint physicsJoint)
    2. {
    3.     var potentialSpindleEntity = physicsJoint.EntityA;
    4.     if (spindleComponentData.Exists(potentialSpindleEntity))
    5.     {
    6.         var spindle = spindleComponentData[potentialSpindleEntity];
    8.         var targetRotRad = spindle.desiredAxisRotationRad;
    9.         var constraints = physicsJoint.JointData.Value.Constraints[0];
    10.         constraints.Min = targetRotRad - 0.01f;
    11.         constraints.Max = targetRotRad + 0.01f;
    12.         physicsJoint.JointData.Value.Constraints[0] = constraints;
    14.         spindleComponentData[potentialSpindleEntity] = spindle;
    15.     }
    16. }
    What's bothering though is the `physicsJoint.JointData.Value.Constraints[0]` – this can't be the right way to set the Min and Max…? Or is it?

    Thanks for any help!

    P.S: Setup of the GO (converted into entities):
    Screenshot 2019-08-29 10.25.57.png
  2. florianhanke


    Jun 8, 2018