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. The 2022.2 beta is now available for testing. To find out what's new, have a look at our 2022.2 feature highlights.
    Dismiss Notice
  3. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here:
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
Dismiss Notice
Submit bug reports tagged with #Beta2022Win_Unity when you encounter unknown issues while testing the 2022.2 betas for a chance to win a year of Unity Pro. For more information, have a look at our Beta Sweepstakes Announcement.

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

Discussion in 'DOTS Physics' 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