Rotatation on battleship not working to well

Hey devs, I’m scripting a battle ship but the turrets dont follow the mouse to well when the ship rotates or moves around.

How can I fix this?

– LOCAL SIDE


– SERVER SIDE

math.rad(mouse * adjust)

CFrame.fromEulerAnglesXYZ expects its arguments in radians not degrees. You probably want to use CFrame.lookAt for this instead as the mouse’s LookVector describes its direction not its orientation.

1 Like

So in this case rotatePart.Weld.C1 = CFrame.lookAt(0,mouse,0)?

Because I dont really know about CFrame.lookAt.

I’ve tried somethings and I came up with this:

On local side:

game.ReplicatedStorage.Turn:FireServer(rotatePart, mouse.Hit.Position)
game.ReplicatedStorage.Turn:FireServer(rotatePart2, mouse.Hit.Position)

On server side:

local adjust = 5 
game.ReplicatedStorage.Turn.OnServerEvent:Connect(function(player,rotatePart,mousePos) 
	local _, Y, _ = CFrame.lookAt(rotatePart.Position, mousePos):ToEulerAnglesXYZ() -- Converts cframe to radians.
	
	rotatePart.Weld.C1 = CFrame.new(rotatePart.Weld.C1.Position) * CFrame.Angles(0, math.rad(Y * (180 * adjust) / math.pi), 0) 
    -- Gets the new position and then sets the rotation in the Y axis
	-- The calculation converts radians to degrees, so it's easier to adjust the sensibility
end)
1 Like

It works but its kinda messed up and still having the game problem :sad:

Ok, then try this

game.ReplicatedStorage.Turn.OnServerEvent:Connect(function(player,rotatePart,mousePos) 
	local newPos = mousePos * Vector3.new(1, 0, 1) + Vector3.new(0, rotatePart.Position.Y, 0)
	
	rotatePart.CFrame = CFrame.lookAt(rotatePart.Position, newPos)
end)

If it doesn’t work, send me a pic of the ship object, to check if it has any constraints or different configuration

2 Likes

Ok, try using WeldConstraint instead of Weld, it seems to work for me

If you want to apply world space weld to a weld you will need to do derive the formula as well, if you want to apply @SirPotato_a CFrame method.

Here is an example when applying to a welds C0.

Another equation I somehow found to also work is this one, derived from the weld equation.

To make a CFrame apply to local space like this formula.

Also I made a module to handle it for additional reference though it’s pretty messy.

2 Likes

So i kinda fixed it but when the ship turns the weapons wont follow the mouse

Can you send us your script?

()

– Server

– Client

Any errors on output?

(making text longer; ignore this)

No, there’s currently no errors.

So I was messing around using some equations and I found out a simpler way of doing this:

  • Client
local rotatePart = --Put here the part in Weld that doesn't change its position even though C0 and C1 values were set
local weld = -- (Optional) Just to do a little bit of protection to your game from people; Set this to the weld

while wait() do

      game.ReplicatedStorage.Turn:FireServer(weld, rotatePart, mouse.Hit.Position)

end
  • Server
game.ReplicatedStorage.Turn.OnServerEvent:Connect(function(player, weld, rotatePart, mousePos)
    if rotatePart then
        if weld.Part1 == rotatePart or weld.Part0 == rotatePart then --Optional
            rotatePart.CFrame = CFrame.lookAt(rotatePart.Position, mouse.Position + Vector3.new(0, -mouse.Position.Y + rotatePart.Position.Y, 0)) -- Since the ship and the weapons are attached, when we move the main part in the weld, everything will move with it and we will be able to fix this issue
        else
    end
end)

Are you sure that you’ve wrote the code correctly and the weld part is facing the same direction as the weapons? The code works perfectly for me

image

I think it has something to do with the welds?