Hinge Joint 2D change motor speed

Hello everyone,

I’m working on Unity 2d project. and using hinge joint 2d to make animation for character.

Here is my code to make animation called “kick”.

public void Jump()
{
//Debug.Log (“Jump”);
if (grounded)
thisRigidbody.AddForce (jumpHeight * m_ThisTransform.up);

//hingeJoint2D.useMotor = false;
//hingeJoint2D = rightLeg.GetComponent ();

JointMotor2D motor = hingeJoint2D.motor;
if (team == Team.Left)
motor.motorSpeed = -motorSpeed;
else
motor.motorSpeed = motorSpeed;

hingeJoint2D.useMotor = true;
hingeJoint2D.motor = motor;

if (grounded)
grounded = false;

Invoke (“ResetLeg”, 0.2f);
}

void ResetLeg()
{
//hingeJoint2D = rightLeg.GetComponent ();
JointMotor2D motor = hingeJoint2D.motor;
if (team == Team.Left)
motor.motorSpeed = motorSpeed;
else
motor.motorSpeed = -motorSpeed;

hingeJoint2D.motor = motor;
//Invoke (“TurnOffMotor”, 0.1f);
}

Method working fine on first using. But when call “Jump” second, motor speed of JointMotor2D doest change.

Please suggest me some solution to fix this issue.

Thanks

Sorry this issue fixed by my selft :smile:

Hi, how did you get the motor speed to change?
I can’t find how. Thanks.

Ah, I found an answer on another thread! Gee, it’s weird and couldn’t find official documentation for it.

1 Like

everyone finds the answer and doesn’t post it here, nice!!

6 Likes

here we go, the solution:

JointMotor2D motor = hingeJoint2D.motor;
motor.motorSpeed = 400;
hingeJoint2D.motor = motor;

7 Likes

Thank you so much, this fixed my problem! None of the other forums seem to have this answer!

@Hasasan-Kanso It shows 2 errors saying:

  1. the name ‘hingeJoint2D’ does not exist in the current context
  2. the name ‘hingeJoint2D’ does not exist in the current context
    Help pls.

You have to define hingeJoint2D
above start put

HingeJoint2D hingeJoint2D;

and in start

hingeJoint2D = gameObject.GetComponent();