Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I keep 2 piece turret rotating and aiming in sync? Lerp is failing me...

Asked by 5 years ago

Hello everyone,

I'm designing a turret that consists of two main components, the base of the turret that only needs to rotate on the Y axis and the barrel of the turret that I need to aim on the X and Z axis.

Posted below is the entire function I have setup for the aiming of the turret, currently I'm trying to keep them in sync through lerping, but the turret doesn't stay together perfectly the parts lag behind each other a bit. I have also tried tweening for this but the lag is even worse.

Another idea I had for this was using welds, but since the CFrame is based off of offset instead of realworld position I couldn't figure out how to rotate the bricks to face the correct way properly.

Does anyone have any idea how this problem could be solved?

Thanks for reading.

function aim(target)
    print("Aim function called")
    aiming = true

    repeat
        wait()
        local look = Vector3.new(target.Position.X,turretBase.Position.Y, target.Position.Z)
        local look2 = Vector3.new(target.Position.X,standBasePivot.Position.Y, target.Position.Z)

        local diff = (turretBase.CFrame.LookVector - CFrame.new(turretBase.CFrame.p,look).LookVector).Magnitude
        --print("turretBase diff:",diff)
        for i=0,1,0.01 do
            wait()

            local x,y,z = standBasePivot.CFrame:ToEulerAnglesXYZ()
            local x1,y1,z1 = standBase.CFrame:ToEulerAnglesXYZ()

            standBase.CFrame = standBase.CFrame:Lerp(CFrame.new(standBasePivot.CFrame.p, target.Position - Vector3.new(0,3,0)),i)

            turretBase.CFrame = CFrame.new(turretBase.CFrame.p) * CFrame.Angles(x1,y1,0)
            --turretBase.CFrame = turretBase.CFrame:Lerp(CFrame.new(turretBase.CFrame.p,look),i)

            --standBase.CFrame = standBase.CFrame:Lerp(CFrame.new(standBasePivot.CFrame.p) * CFrame.Angles(x1,y,z1),i)          
            if i > 0.3 then
                break
            end
        end
        local diff = (standBase.CFrame.LookVector - (CFrame.new(standBasePivot.CFrame.p, target.Position - Vector3.new(0,3,0))).LookVector).Magnitude
        --print("standBase diff:",diff)
        --[[for i=0,1,0.01/diff do
            wait()
            --turretBase.CFrame = turretBase.CFrame:Lerp(CFrame.new(turretBase.CFrame.p,look),i)
            standBase.CFrame = standBase.CFrame:Lerp(CFrame.new(standBasePivot.CFrame.p, target.Position - Vector3.new(0,3,0)),i)
            if i > 0.3 then
                break
            end
        end--]]
        local x,y,z = standBase.CFrame:ToEulerAnglesXYZ()
        print("X:",x,"Y",y,"Z",z)
        local dist = (myTorso.Position - target.Position).magnitude
    until target == nil or checkSight(target) == false or target.Parent.Humanoid.Health < 1 or dist < 10 or dist > searchRadius
    aiming = false
    print("Finished aiming...")
end

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The server side of it can be a little bit laggy it doesn't matter, what you are really doing is making a visual feature for the player. If you animate it on the client side you can allow it to update a lot faster without have the server's performance suffer. If you change the rotation/position of the two parts at the same time then you should theoretically get it to sync together by using this method.

Ad

Answer this question