Cannon doesn't follow aiming ui when ship is turning

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want cannon to be fixed to aiming ui on mobile

  2. What is the issue? Include screenshots / videos if possible!
    Cannon barrel is behind the aiming ui when ship is turning.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried fixing the code.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- workspace.CurrentCamera.Changed:Connect(function()
        if not currentShip or currentGun == 0 or usingtorpedo then return end
        if not currentShip:FindFirstChild("Gun" .. currentGun) then return end
        
        local gunModel = currentShip:FindFirstChild("Gun" .. currentGun);

        local aimVector = ((workspace.CurrentCamera.CFrame + workspace.CurrentCamera.CFrame.LookVector * 50).Position - gunModel.Forward.Position).Unit
        local xzVector = (Vector3.new(workspace.CurrentCamera.CFrame.Position.X, gunModel.Forward.Position.Y, workspace.CurrentCamera.CFrame.Position.Z) - gunModel.Forward.Position).Unit
        local lookVector = (gunModel.Forward.CFrame + workspace.CurrentCamera.CFrame.LookVector * 50).LookVector
        
        local lookAt = gunModel.Forward.Position + lookVector

        local xzAngle = math.atan2(xzVector.Z, xzVector.X) - math.atan2(lookVector.Z, lookVector.X)

        if currentGun == 2 then
            currentShip.Center["Gun" .. currentGun].C1 = CFrame.fromOrientation(0, xzAngle, 0) * CFrame.Angles(0, math.rad(-180), 0)
            gunModel.Base.Point.WorldCFrame = workspace.CurrentCamera.CFrame;

        elseif currentGun == 3 then
            gunModel.Stand.Lower.C1 = CFrame.fromOrientation(0, xzAngle, 0)
        else
            currentShip.Center.Stand.C1 = CFrame.fromOrientation(0, xzAngle, 0) * CFrame.Angles(0, math.rad(-180), 0);
        end
        
        local VerticalAimVector = aimVector.Y;
        
        if currentGun == 2 and not AimingCannon then VerticalAimVector = 0 end
        
        local verticalAngle = math.asin(VerticalAimVector - gunModel.Forward.CFrame.LookVector.Y)
        gunModel.Base.Aim.C1 = CFrame.fromOrientation(-verticalAngle, 0, 0)

        horizontalAngle = xzAngle
        heightAngle = -verticalAngle
    end)

Camera type: track

1 Like