Trying to program bones that "drag" (jiggle bone but simpler).

I’m a noob when it comes to unity and I’m trying to program limb drag. (basically dynamic bone or jiggle, but simpler)

However it’s more or less not working at all (bone stays at a weird angle and doesn’t move), and there’s probably something blatantly wrong with the concept. Any idea what I’m doing wrong (or horribly misunderstanding.)

public class limb_drag : MonoBehaviour
{
    Vector3 prev_child_position;
    Vector3 target_position;
    Quaternion delay_rot;
    Quaternion prev_rotation;
    public float delay_amount;
    public Transform child;
    // Start is called before the first frame update
    void Start()
    {
       
        child = transform.GetChild(0);
        prev_child_position = child.transform.position;
        delay_rot = Quaternion.identity;
        prev_rotation = transform.rotation;
    }

    // Update is called once per frame
    void Update()
    {
        //undo delay from last step
        transform.rotation = transform.rotation * Quaternion.Inverse(delay_rot);
       
        ///save position of the end of this bone
        prev_child_position = child.transform.position;

        ///save current rotation of bone
        prev_rotation = transform.rotation;

        ///look at the saved position of the child bone from the last step
        transform.LookAt(prev_child_position);

        //lerp between the orriginal rotation and the look rotation by 0.1 or so
        delay_rot = Quaternion.Lerp(prev_rotation, transform.rotation, delay_amount);
       
        ///set rotation back to before look
        transform.rotation = prev_rotation;

        ///rotate using lerped value
        transform.rotation = transform.rotation * delay_rot;

    }
}

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

It’s not that it doesn’t run, it’s just that I know I probably have some very specific lack of understanding about the things I’m using there. Likely a difference between global and local rotation.

Does not sound very specific.

“weird angle” and “doesn’t move” cannot be fixed.

That was why I suggested the approach above, to help you narrow it down, get some hard numbers.

I assure you nobody here has any numbers they can tell you.

Well, to narrow it down, is there a reason why this code makes the bone flip around like crazy?
It should rotate, and then rotate back, so I’m assuming what I’m misunderstanding here is playing a part in my overall problem.

        drag_undo = Quaternion.identity;
    }

    // Update is called once per frame
    void Update()
    {
        transform.rotation = transform.rotation * Quaternion.Inverse(drag_undo);

        prev_rotation = transform.rotation;
        transform.Rotate(drag_x_offset, drag_y_offset, drag_z_offset);
        drag_undo = Quaternion.Lerp(prev_rotation,transform.rotation, 1f);
}