I'm making a mech, and I need to learn some things.

I got the legs to always touch the ground below, but I then tried to modify the right leg to point to the ground, but on the xy of a target. This failed miserably and my code is getting messier and messier...

Code is: (so you don't think I'm just asking you to make a mech for me, I'm trying to learn where I fail, and maybe some optimisation tips ^^)
Code:
@name Holo test model
@outputs [Base]:entity
##Adjust##
@outputs [OffsetRightJoint OffsetLeftJoint]:vector
@outputs TransUnit

##holos##
@outputs [HoloModel HoloPos HoloScale HoloAng]:array

##Legs##
@outputs [LeftLegTarget RightLegTarget RightLegV LeftLegV]:vector

##ranger##
@outputs [RightR LeftR DownR]:ranger
@outputs RDist LDist

if (first()|duped())
{
    runOnTick(1)
    TransUnit = 12^(-1)
    ##Base##
    Base = entity():isWeldedTo()
    
    ##Leg joints##
    ##right leg joint##
    HoloModel[1,string] = "sphere3"
    HoloPos[1,vector] = Base:toWorld(OffsetRightJoint)
    OffsetRightJoint = vec(10,80,0)
    HoloScale[1,vector] = vec(2,2,2)
    HoloAng[1,angle] = ang(0,0,0)
    
    
    
    ##left leg joint##
    HoloModel[2,string] = "sphere3"
    HoloPos[2,vector] = Base:toWorld(OffsetLeftJoint)
    OffsetLeftJoint = vec(10,-80,0)
    HoloScale[2,vector] = vec(2,2,2)
    HoloAng[2,angle] = ang(0,0,0)
    
    
    
    ##Legs##
    ##right leg##
    HoloModel[3,string] = "cone"
    RightR = rangerOffset(500000,holoEntity(1):pos(),vec(0,0,-1))
    RDist = RightR:distance()
    HoloScale[3,vector] = vec(1,1,RDist/2*TransUnit)
    HoloPos[3,vector] = holoEntity(1):pos()
    HoloAng[3,angle] = ang(180,0,0)
    holoCreate(3,HoloPos[3,vector],HoloScale[3,vector],ang(0,0,0),vec(255,255,255),HoloModel[3,string])
    
    ##left leg##
    HoloModel[4,string] = "cone"
    LeftR = rangerOffset(500000,holoEntity(2):pos(),vec(0,0,-1))
    LDist = LeftR:distance()
    HoloScale[4,vector] = vec(1,1,LDist/2*TransUnit)
    HoloPos[4,vector] = holoEntity(2):pos()
    HoloAng[4,angle] = ang(180,0,0)
    holoCreate(4,HoloPos[4,vector],HoloScale[4,vector],ang(0,0,0),vec(255,255,255),HoloModel[4,string])
    
    
    
    ##holoCreate##
    holoCreate(1,HoloPos[1,vector],HoloScale[1,vector],ang(0,0,0),vec(255,255,255),HoloModel[1,string])
    holoCreate(2,HoloPos[2,vector],HoloScale[2,vector],ang(0,0,0),vec(255,255,255),HoloModel[2,string])
    
    
    
    ##Debugg##
    
    RightR = rangerOffset(500000,holoEntity(1):pos(),(holoEntity(1):pos()-RightLegTarget))
    print(RightR:position():toString())
}

##BASE##
DownR = rangerOffset(500000,Base:pos(),vec(0,0,-1))
RightLegTarget = owner():pos()

##JOINTS##
HoloPos[1,vector] = Base:toWorld(OffsetRightJoint)
HoloPos[2,vector] = Base:toWorld(OffsetLeftJoint)
holoPos(1,HoloPos[1,vector])
holoPos(2,HoloPos[2,vector])

##LEGS##
##right##
RightR = rangerOffset(500000,holoEntity(1):pos(),(holoEntity(1):pos()-RightLegTarget):normalized())
RightLegV = vec(RightR:position():x(),RightR:position():y(),DownR:position():z())
RDist = RightLegV:distance(holoEntity(1):pos())
HoloScale[3,vector] = vec(1,1,RDist*TransUnit)
HoloPos[3,vector] = holoEntity(1):pos()+(vec(0,0,-1))*RDist/2
HoloAng[3,angle] = (holoEntity(1):pos()-RightLegV):toAngle()+ang(180,0,0)
holoPos(3,HoloPos[3,vector])
holoScale(3,HoloScale[3,vector])
holoAng(3,HoloAng[3,angle])

##left##
LeftR = rangerOffset(500000,holoEntity(2):pos(),vec(0,0,-1))
LDist = LeftR:distance()
HoloScale[4,vector] = vec(1,1,LDist*TransUnit)
HoloPos[4,vector] = holoEntity(2):pos()+(vec(0,0,-1))*LDist/2
holoPos(4,HoloPos[4,vector])
holoScale(4,HoloScale[4,vector])
holoAng(4,HoloAng[4,angle])