1

我正在尝试制作一个可以单独移动每个肢体的布娃娃。然而,当我尝试在布娃娃的右腿上进行测试时,我得到了这个错误。

error CS1061:'CharacterJoint' does not contain a definition for 'AddForce' and no accessible extension method 'AddForce' accepting a first argument of type 'CharacterJoint' could be found (are you missing a using directive or an assembly reference?)

我尝试用 SwingAxis 切换 AddForce,但没有任何改变。我查找了 Character Joint 的方法,但找不到任何会使腿在关节处弯曲的方法。

public class rightLeg_movement : MonoBehaviour
 {
     public CharacterJoint cj;
     public float forwardForce = 2000f;

     void FixedUpdate()
     {
         if( Input.GetKey("m") )
         {
             cj.AddForce(forwardForce, 0, 0);
         }
     }
 }

我查找的所有教程都使用动画。但是我计划实施一个人工智能来完成布偶的基本任务(比如学习自己捡起来,或者学习走路)。

4

1 回答 1

2

CharacterJoints 不支持驱动内置关节。你最好使用ConfigurableJoints 代替。一旦配置了关节的限制,您就可以使用ConfigurableJoint.angularXDrive它来配置它可以应用的最大角力,然后ConfigurableJoint.targetAngularVelocity配置目标角速度。

此外,建议在可能的情况下调用Input方法。从文档中:UpdateFixedUpdate

另请注意,输入标志直到Update. Update建议您在循环中进行所有输入调用。

总而言之,对于只能围绕局部 x 轴移动的膝关节,这些变化可能看起来像这样:

public class rightLeg_movement : MonoBehaviour
{

    public ConfigurableJoint cj;

    public float forwardForce = 2000f;
    public float backwardForce = 1000f;
    public float holdForce = 5000f;

    public float forwardVelocity = 2f;
    public float backwardVelocity = 1f;

    private bool isInputForward = false;
    private bool isInputBackward = false;

    void Start()
    {
        SetForwardJointMovement(holdForce, 0f);
    }

    void Update()
    {
        isInputForward = Input.GetKey("m");
        isInputBackward = Input.GetKey("n");

        if (isInputForward)
        {
            SetForwardJointMovement(forwardForce, forwardVelocity);
        }
        else if (isInputBackward)
        {
            SetForwardJointMovement(backwardForce, -backwardVelocity);
        }
        else
        {
            SetForwardJointMovement(holdForce, 0f);
        }
    }

    void SetForwardJointMovement(float force, float velocity)
    {
        JointDrive drive = new JointDrive();
        drive.maximumForce = force;
        drive.positionDamper = 1e10f;
        cj.angularXDrive = drive;

        cj.targetAngularVelocity = Vector3.left * velocity;
    }
}

的文档ConfigurableJoint目前处于非常糟糕的状态,因此如果出现问题,我们深表歉意。

于 2019-09-06T21:42:02.477 回答