The problem with the motor HingeJoint2D

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.

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.

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.

1 Like

Thank you so much for your help. Your advice helped. As a result , the code began to look like this

    public void ControlBackBoard(bool mode)
    {
        if (mode)
        {
            JointMotor2D BoardMotor = new JointMotor2D { motorSpeed = 1000f, maxMotorTorque = 2000f };
            BoardCar.motor = BoardMotor;

            if (BoardTrailer != null)
            {
                BoardTrailer.motor = BoardMotor;
                BoardTrailer.GetComponent<Rigidbody2D>().WakeUp();
            }
        }
        else
        {
            JointMotor2D BoardMotor = new JointMotor2D { motorSpeed = -1000f, maxMotorTorque = 1000f };
            BoardCar.motor = BoardMotor;
            if (BoardTrailer != null)
            {
                BoardTrailer.motor = BoardMotor;
                BoardTrailer.GetComponent<Rigidbody2D>().WakeUp();
            }
        }
        BoardCar.GetComponent<Rigidbody2D>().WakeUp();
    }

Well, I additionally added a check for end positions. It doesn’t work normally without this

    private void FixedUpdate()
    {
        if (BoardCar.limitState == JointLimitState2D.LowerLimit && BoardCar.motor.motorSpeed < 0f)
        {
            var motor = BoardCar.motor;
            motor.motorSpeed = 0;
            BoardCar.motor = motor;
        }

        if (BoardCar.limitState == JointLimitState2D.UpperLimit && BoardCar.motor.motorSpeed > 0f)
        {
            var motor = BoardCar.motor;
            motor.motorSpeed = 0;
            BoardCar.motor = motor;
        }

        if (BoardTrailer != null)
        {
            if (BoardTrailer.limitState == JointLimitState2D.LowerLimit && BoardTrailer.motor.motorSpeed < 0f)
            {
                var motor = BoardTrailer.motor;
                motor.motorSpeed = 0;
                BoardTrailer.motor = motor;
            }
            if (BoardTrailer.limitState == JointLimitState2D.UpperLimit && BoardTrailer.motor.motorSpeed > 0f)
            {
                var motor = BoardTrailer.motor;
                motor.motorSpeed = 0;
                BoardTrailer.motor = motor;
            }

        }