• Unity
  • What is the right way to set WorldToLocal position?

  • Editado
Related Discussions
...
void Update()
{
Vector2 localPosition;

SomeBone.WorldToLocal(worldPosition.x, worldPosition.y, out localPosition.x, out localPosition.y);
SomeBone.SetPositionSkeletonSpace(localPosition);
}

I have no keyframes for this bone, tracks are cleared, but the bone jitters between initial position and position i want to set. What am i doing wrong?

if i'm doing this

SomeBone.SetLocalPosition(localPosition);

my bone is flying around

adding

skeleton.UpdateWorldTransform();

doesnt help

Harald escribió

You should not call the code in Update(), instead call it in the respective UpdateLocal/UpdateWorld/UpdateComplete delegates as shown here:
Skeleton Utility Bone Relative/Additive override?

void Start()
    {
        skeletonAnimation = GetComponent<SkeletonAnimation>();
      skeletonAnimation.UpdateLocal += MoveBoneUpdate;

  skeleton = skeletonAnimation.Skeleton;

    bone = skeleton.FindBone("jaw");

  //Vector2 localPos;
  //bone.WorldToLocal(0, 0, out localPos.x, out localPos.y);
  //bone.SetLocalPosition(localPos);
  //bone.SetPositionSkeletonSpace(localPos);

   }

   void MoveBoneUpdate(ISkeletonAnimation s)
    {
      Vector2 localPos;
      bone.WorldToLocal(0, 0, out localPos.x, out localPos.y);
      bone.SetLocalPosition(localPos);

  //bone.SetPositionSkeletonSpace(localPos);
  //skeletonAnimation.skeleton.UpdateWorldTransform();
   }

I did like you said, but the bone is still flying around. Here is example project https://send.firefox.com/download/f144fd3fe2ddebd9/#NL7_uoUmwsbbqLnI-a4NJg

If i am setting local position only once at Start() it works well only with bone.SetPositionSkeletonSpace(localPos), but SetLocalPosition is setting the bone on wrong place, take a look. Just for notice, parent bone is rotated and i think it causes wrong position.

Thanks for sending the reproduction project.

I'm not sure why it behaves incorrectly using your code stated above, but the following code works to set the bone to a custom world position:

void MoveBoneUpdate(ISkeletonAnimation s)
{
    Vector3 worldPos = Vector3.zero;
    bone.SetPositionSkeletonSpace(this.transform.InverseTransformPoint(worldPos));
}

Note that bone.SetPositionSkeletonSpace() takes position in the skeleton's space, not local bone space.