Search Unity

Hinge Joint 2D change motor speed

Discussion in '2D' started by newlightgame01, May 14, 2016.

  1. newlightgame01

    newlightgame01

    Joined:
    Dec 8, 2015
    Posts:
    3
    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<HingeJoint2D> ();

    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<HingeJoint2D> ();
    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
     
  2. newlightgame01

    newlightgame01

    Joined:
    Dec 8, 2015
    Posts:
    3
    Sorry this issue fixed by my selft :D
     
  3. tarabout

    tarabout

    Joined:
    Jan 23, 2016
    Posts:
    11
    Hi, how did you get the motor speed to change?
    I can't find how. Thanks.
     
  4. tarabout

    tarabout

    Joined:
    Jan 23, 2016
    Posts:
    11
    Ah, I found an answer on another thread! Gee, it's weird and couldn't find official documentation for it.
     
    Ishimaeru likes this.
  5. Hassan-Kanso

    Hassan-Kanso

    Joined:
    Jun 10, 2014
    Posts:
    11
    everyone finds the answer and doesn't post it here, nice!!
     
  6. Hassan-Kanso

    Hassan-Kanso

    Joined:
    Jun 10, 2014
    Posts:
    11
    here we go, the solution:

    JointMotor2D motor = hingeJoint2D.motor;
    motor.motorSpeed = 400;
    hingeJoint2D.motor = motor;
     
    ncr100, Dizlen, SPors697 and 4 others like this.
  7. Sapphiros

    Sapphiros

    Joined:
    Jan 28, 2020
    Posts:
    1
    Thank you so much, this fixed my problem! None of the other forums seem to have this answer!
     
  8. Curiosity_Tube

    Curiosity_Tube

    Joined:
    Oct 2, 2020
    Posts:
    2
    @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.
     
  9. jebu_

    jebu_

    Joined:
    May 18, 2019
    Posts:
    2
    You have to define hingeJoint2D
    above start put

    HingeJoint2D hingeJoint2D;

    and in start

    hingeJoint2D = gameObject.GetComponent<HingeJoint2D>();