Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question The problem with the motor HingeJoint2D

Discussion in 'Physics' started by GadOleg, Sep 26, 2023.

  1. GadOleg

    GadOleg

    Joined:
    Nov 13, 2019
    Posts:
    58
    Hello! I have a problem with hingejoint2d. With his help, I'm trying to implement the opening and closing of the trunk lid of the car. To do this, I change the jointmotor, but nothing happens. To get started, you need to somehow influence the door, after that the motor begins to perform the action. Why is this happening? this is extremely inconvenient.
     
    Last edited: Sep 26, 2023
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,388
    stupid as it sounds think how it works in real life, if you open your boot you dont mind if its assisted but you want it to stay open until you're done, so you cant have permenant motor running. You would need to maybe start the close for it to take over.

    Why would that be different on your unity object? whatever angles you asked the motor to kick in from you would end the same behavior either it would never stay open or it wont close till you tell it.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    It might be that the bodies in question are asleep. Unless you're actually changing the values of the motor speed or the max motor torque or the use-motor option, it won't wake the bodies.

    A workaround for this is to explicitly wake the Rigidbody2D the joint is on or disable/enable the motor usage which does it indirectly.

    You can see the Rigidbody2D Sleep-State in the "Info" fold-out if you wish to monitor it. Also, the collider gizmos have a different color indicating sleep/awake.
     
    Unifikation likes this.
  4. GadOleg

    GadOleg

    Joined:
    Nov 13, 2019
    Posts:
    58
    Thank you so much for your help. Your advice helped. As a result , the code began to look like this
    Code (CSharp):
    1.     public void ControlBackBoard(bool mode)
    2.     {
    3.         if (mode)
    4.         {
    5.             JointMotor2D BoardMotor = new JointMotor2D { motorSpeed = 1000f, maxMotorTorque = 2000f };
    6.             BoardCar.motor = BoardMotor;
    7.  
    8.             if (BoardTrailer != null)
    9.             {
    10.                 BoardTrailer.motor = BoardMotor;
    11.                 BoardTrailer.GetComponent<Rigidbody2D>().WakeUp();
    12.             }
    13.         }
    14.         else
    15.         {
    16.             JointMotor2D BoardMotor = new JointMotor2D { motorSpeed = -1000f, maxMotorTorque = 1000f };
    17.             BoardCar.motor = BoardMotor;
    18.             if (BoardTrailer != null)
    19.             {
    20.                 BoardTrailer.motor = BoardMotor;
    21.                 BoardTrailer.GetComponent<Rigidbody2D>().WakeUp();
    22.             }
    23.         }
    24.         BoardCar.GetComponent<Rigidbody2D>().WakeUp();
    25.     }
    Well, I additionally added a check for end positions. It doesn't work normally without this
    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         if (BoardCar.limitState == JointLimitState2D.LowerLimit && BoardCar.motor.motorSpeed < 0f)
    4.         {
    5.             var motor = BoardCar.motor;
    6.             motor.motorSpeed = 0;
    7.             BoardCar.motor = motor;
    8.         }
    9.  
    10.         if (BoardCar.limitState == JointLimitState2D.UpperLimit && BoardCar.motor.motorSpeed > 0f)
    11.         {
    12.             var motor = BoardCar.motor;
    13.             motor.motorSpeed = 0;
    14.             BoardCar.motor = motor;
    15.         }
    16.  
    17.         if (BoardTrailer != null)
    18.         {
    19.             if (BoardTrailer.limitState == JointLimitState2D.LowerLimit && BoardTrailer.motor.motorSpeed < 0f)
    20.             {
    21.                 var motor = BoardTrailer.motor;
    22.                 motor.motorSpeed = 0;
    23.                 BoardTrailer.motor = motor;
    24.             }
    25.             if (BoardTrailer.limitState == JointLimitState2D.UpperLimit && BoardTrailer.motor.motorSpeed > 0f)
    26.             {
    27.                 var motor = BoardTrailer.motor;
    28.                 motor.motorSpeed = 0;
    29.                 BoardTrailer.motor = motor;
    30.             }
    31.  
    32.         }