Inverse Kinematics Leg Y Axis (Unity)

31 Views Asked by At

so i was working on an inverse kinematic leg script in unity, that moves the leg forward when it's left behind a certain distance, problem is, over the course of the lerp to the new point i want to move the leg up and down the y axis like a real leg (the highest point is reached in the middle between the two points) also there is an addition i coded in that moves the leg in the opposite direction the leg came from so that has to be included in this logic as well

i can't get it working can someone help me with that? my brain can't comprehend how to do that rn

here is my script without this logic i ask help for

using UnityEngine;

public class Leg : MonoBehaviour
{
    public Transform legTargetTransform;
    public Transform legTipTransform;
    public Transform legRootTransform;
    public Transform fixedMiddle;
    public Vector3 RaycastPosition;
    public LayerMask ignoreLayer;
    public float downRaycastDistance = 1f;
    public float moveDistance = 0.1f;
    public float addition;
    public Rigidbody CoreRb;
    public float transitionSpeed;

    private RaycastHit hitInfo;
    private Vector3 lastPosition;
    bool transition;

    void Start()
    {
        fixedMiddle.transform.position = legTipTransform.position;
        SetPoint(0);
    }

    void Update()
    {
        

        if(transition)
        {
            legTargetTransform.position = Vector3.Lerp(legTargetTransform.position, RaycastPosition, transitionSpeed); 
            {
                transition = false;
            }
        }
        else
        {
            legTargetTransform.position = RaycastPosition;
        }

        // Check if the leg tip has moved significantly
        if (Vector3.Distance(fixedMiddle.position, legTargetTransform.position) > moveDistance)
        {
            Debug.Log("OUT OF RANGE");
            SetPoint(addition);
        }
    }

void SetPoint(float addition)
{
    if (Physics.Raycast(legRootTransform.position, Vector3.down, out hitInfo, downRaycastDistance, ~ignoreLayer))
    {
        Vector3 direction = (transform.position - lastPosition).normalized;
        direction.y = 0f;
        RaycastPosition = hitInfo.point + direction * addition;
        lastPosition = hitInfo.point;
        transition = true;
    }
}
}


0

There are 0 best solutions below