代码之家  ›  专栏  ›  技术社区  ›  SonicBlue22

为什么在世界空间中查找HingeJoint2D.anchor时得到的向量不正确?

  •  1
  • SonicBlue22  · 技术社区  · 7 年前

    在这个场景中,我有一长串的孩子通过铰链连接到他们的父母。对于我的代码,我需要世界空间中铰链锚的位置,因此我使用:

    public Vector2 hingeVector => hinge.anchor + (Vector2)gameObject.transform.position;
    

    对于第一个铰链,该代码给出了正确的位置。但对于第二个铰链,这种情况会发生:

    anchor position I get (red) vs actual anchor (blue)

    1 回复  |  直到 7 年前
        1
  •  1
  •   Community Mohan Dere    6 年前

    锚定值为 和不受旋转的影响,但是为了计算世界空间中的定位点,知道变换的位置,需要通过对象的旋转来旋转定位点值 然后

    Vector2 p = hinge.anchor.Rotate(gameObject.transform.rotation.eulerAngles.z)
                  + (Vector2)gameObject.transform.position;