Search Unity

Question Yet another constraint problem

Discussion in 'Physics for ECS' started by hardcodednumber, Jan 28, 2022.

  1. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    I've tried to use google to find the answer to this, but I keep finding several different 'answers'. I want to constrain my player on the Z, and its rotation. From what I've seen I've created this script:

    Code (CSharp):
    1.  public sealed class PhysicsConstraint : MonoBehaviour, IConvertGameObjectToEntity
    2.     {
    3.         public bool3 Linear = new bool3();
    4.         public bool3 Angular = new bool3();
    5.  
    6.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    7.         {
    8.             var entityA = new RigidTransform(
    9.                 dstManager.GetComponentData<Rotation>(entity).Value,
    10.                 dstManager.GetComponentData<Translation>(entity).Value
    11.             );
    12.             RigidTransform entityB = new RigidTransform(float3x3.identity, float3.zero);
    13.  
    14.             PhysicsJoint joint = default(PhysicsJoint);
    15.             joint.BodyAFromJoint = entityA;
    16.             joint.BodyBFromJoint = entityB;
    17.             joint.JointType = JointType.Custom;
    18.             joint.SetConstraints(new FixedList128<Constraint> {
    19.              new Constraint
    20.                 {
    21.                     ConstrainedAxes = Linear,
    22.                     Type = ConstraintType.Linear,
    23.                     Min = 0,
    24.                     Max = 0,
    25.                     SpringFrequency = Constraint.DefaultSpringFrequency,
    26.                     SpringDamping = Constraint.DefaultSpringDamping
    27.                 },
    28.                 new Constraint
    29.                 {
    30.                     ConstrainedAxes = Angular,
    31.                     Type = ConstraintType.Angular,
    32.                     Min = 0,
    33.                     Max = 0,
    34.                     SpringFrequency = Constraint.DefaultSpringFrequency,
    35.                     SpringDamping = Constraint.DefaultSpringDamping
    36.                 }
    37.             });
    38.  
    39.             PhysicsConstrainedBodyPair pair = new PhysicsConstrainedBodyPair(entity, Entity.Null, false);
    40.  
    41.             dstManager.AddComponentData(entity, joint);
    42.             dstManager.AddComponentData(entity, pair);
    43.         }
    44.     }
    When I move around, for the most part, this works. However, if I snag on a box it will force my player to move in the Z axis.
     
  2. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    I removed the second entity part, and it kinda works. But I am still able to move the player in the Z-axis
    Code (CSharp):
    1.     public sealed class PhysicsConstraint : MonoBehaviour, IConvertGameObjectToEntity
    2.     {
    3.         public bool3 Linear = new bool3();
    4.         public bool3 Angular = new bool3();
    5.  
    6.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    7.         {
    8.             var entityA = new RigidTransform(
    9.                 dstManager.GetComponentData<Rotation>(entity).Value,
    10.                 dstManager.GetComponentData<Translation>(entity).Value
    11.             );
    12.  
    13.             PhysicsJoint joint = default(PhysicsJoint);
    14.             joint.BodyAFromJoint = entityA;
    15.             joint.JointType = JointType.Custom;
    16.             joint.SetConstraints(new FixedList128<Constraint> {
    17.              new Constraint
    18.                 {
    19.                     ConstrainedAxes = Linear,
    20.                     Type = ConstraintType.Linear,
    21.                     Min = 0,
    22.                     Max = 0,
    23.                     SpringFrequency = Constraint.DefaultSpringFrequency,
    24.                     SpringDamping = Constraint.DefaultSpringDamping
    25.                 },
    26.                 new Constraint
    27.                 {
    28.                     ConstrainedAxes = Angular,
    29.                     Type = ConstraintType.Angular,
    30.                     Min = 0,
    31.                     Max = 0,
    32.                     SpringFrequency = Constraint.DefaultSpringFrequency,
    33.                     SpringDamping = Constraint.DefaultSpringDamping
    34.                 }
    35.             });
    36.  
    37.             PhysicsConstrainedBodyPair pair = new PhysicsConstrainedBodyPair(entity, Entity.Null, false);
    38.  
    39.             dstManager.AddComponentData(entity, joint);
    40.             dstManager.AddComponentData(entity, pair);
    41.         }
    42.     }
     
  3. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    If anyone cares, I wasn't moving the body by physics. Hence why nothing was working. So this became a rubber ducky moment.
     
    Lukas_Kastern likes this.