I am making a procedural animation of a worm. I have already something working:

enter image description here

(the green dot is the position Target, purple and red are the Head and the Tail of the Worm)

I would like to add another dot in the center of the Worm to simulate the middle body waving up and down while Head and Tail are moving like this:

enter image description here

I thought that it was going to be easy using some kind of 2D joint, but I tried several and I was not able to find a good result.

I was also thinking that maybe I have to make some trigonometry to calculate the possible positions of Center:

enter image description here

Before I over-engineer the solution what would be your approach to solve this?

  • 1
    \$\begingroup\$ It seems your looking for inverse kinematics. Here is a video that might help: youtube.com/watch?v=qqOAzn05fvk. This tutorial is for 3d but it can easily be changed for 2d. \$\endgroup\$
    – Simonster
    Mar 30 at 17:55
  • \$\begingroup\$ @Simonster This helped, I'll use it if my worm becomes more complex :) \$\endgroup\$
    – fguillen
    Mar 30 at 18:39

My final approach has been using basic trigonometry:

enter image description here

I have a and b I was just needing to calculate c:

void MoveCenter()
    float a = maxDistance / 2.0f;
    float b = Vector3.Distance(head.position, tail.position) / 2.0f;
    float c = Mathf.Sqrt(Mathf.Pow(a, 2.0f) - Mathf.Pow(b, 2.0f));

    Vector3 headTailCenterPosition = (head.position + tail.position) / 2.0f;
    Vector3 centerTargetPosition = new Vector3(headTailCenterPosition.x, headTailCenterPosition.y + c, headTailCenterPosition.z);

    center.position = Vector3.MoveTowards(center.position, centerTargetPosition, speed * Time.deltaTime);

The result:

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.