Search Unity

How to stop a constraint in position

Discussion in 'Physics' started by Necrosoft, Sep 22, 2018.

  1. Necrosoft

    Necrosoft

    Joined:
    Oct 27, 2017
    Posts:
    7
    Hi

    Sorry for this noob question, but I'm in trouble working with joints and motor.

    For example, I have an hinge joint controlled by a motor. I can apply a force and velocity by keyboard but I need to set in position the hinge when I don't apply any force yet.

    In order to do that I'm trying to set the limits at the same value in base of current x rotation like that:

    Code (CSharp):
    1. void FixedUpdate () {
    2.         float l_move_h = Input.GetAxis("Horizontal");
    3.         float l_curr_limit = 0;
    4.  
    5.         JointMotor l_jm = m_hinge.motor;
    6.         JointLimits l_jl = m_hinge.limits;
    7.  
    8.         if(l_move_h != 0)
    9.         {
    10.             l_jm.force = 50.0f * Mathf.Abs(l_move_h);
    11.             l_jm.targetVelocity = 10.0f * l_move_h;
    12.             m_hinge.motor = l_jm;
    13.  
    14.             l_jl.min = -30;
    15.             l_jl.max = 30;
    16.             m_hinge.limits = l_jl;
    17.         }
    18.  
    19.         else
    20.         {
    21.             l_jm.force = 0.0f;
    22.             l_jm.targetVelocity = 0.0f;
    23.             m_hinge.motor = l_jm;
    24.  
    25.             l_curr_limit = m_cube2.transform.rotation.eulerAngles.x;
    26.             l_jl.min = l_curr_limit;
    27.             l_jl.max = l_curr_limit;
    28.             m_hinge.limits = l_jl;
    29.         }
    30.     }
    ...But the rotation seems to be resetted at 0 before the limits application. Can you suggest to me the correct procedure to hold in position the joint?

    Many thanks, bye