IK legs synced

Hello! I have a script for my legs which is used to make the legs stick to the ground and when they stray too far, the target of the IK gets put in front. Now I have two problems.

  1. The legs immediately get teleported to the front of the character and I would want them to look normal, you know? I tried using Lerp but it didn’t work.

  2. The legs are synced when walking, I tried giving both different maximum distances from the body but it didn’t work as the character walks towards you, which means he also rotates.

Here is the code

using UnityEngine;

public class WalkAnimFeetIK : MonoBehaviour
{
    public float MaxDistFromBody;
    float RightRealMaxDistFromBody, LeftRealMaxDistFromBody;
    public LayerMask Ground;
    public Transform FeetTargetRight, FeetTargetLeft, TargetForwardRightFoot, TargetForwardLeftFoot;
    float TimerRight, TimerLeft;
    Vector3 InitialPosFootLeft, InitialPosFootRight;
    void Start() {
        InitialPosFootLeft = FeetTargetLeft.position;
        InitialPosFootRight = FeetTargetRight.position;
        LeftRealMaxDistFromBody = MaxDistFromBody;
        RightRealMaxDistFromBody = MaxDistFromBody;
        
    }
    // Update is called once per frame
    void Update()
    {
        FeetTargetLeft.position = InitialPosFootLeft;
        FeetTargetRight.position = InitialPosFootRight;
        TimerRight -= Time.deltaTime;
        if (TimerRight <= 0) {
            RaycastHit Hit;
            Physics.Raycast(TargetForwardRightFoot.position, -Vector3.up, out Hit, Ground);
            
            FeetTargetRight.position = Hit.point;
            TimerRight = 3;
        }
        if (Vector3.Distance(FeetTargetRight.position, transform.position) >= RightRealMaxDistFromBody) {
            TimerRight = 3;
            RaycastHit Hit;
            Physics.Raycast(TargetForwardRightFoot.position, TargetForwardRightFoot.forward, out Hit, Ground);
            RightRealMaxDistFromBody = MaxDistFromBody;
            
            FeetTargetRight.position = Hit.point;
        }
        TimerLeft -= Time.deltaTime;
        if (TimerLeft <= 0) {
            RaycastHit Hit;
            Physics.Raycast(TargetForwardLeftFoot.position, -Vector3.up, out Hit, Ground);
            
            FeetTargetLeft.position = Hit.point;
            TimerLeft = 3;
        }
        if (Vector3.Distance(FeetTargetLeft.position, transform.position) >= LeftRealMaxDistFromBody) {
            TimerLeft = 3;
            RaycastHit Hit;
            Physics.Raycast(TargetForwardLeftFoot.position, TargetForwardLeftFoot.forward, out Hit, Ground);
            LeftRealMaxDistFromBody = MaxDistFromBody;
            
            FeetTargetLeft.position = Hit.point;
        }
        
        InitialPosFootLeft = FeetTargetLeft.position;
        InitialPosFootRight = FeetTargetRight.position;
    }
}

Video :woman_in_manual_wheelchair: