Rotate a part using mouse (Y axis)

also I’m kinda confused with you saying Y, thats up and down, are you trying to make the turret point up and down, or left and right?

You need to make the difference in Y be zero. You can do that by setting them both to zero (multiply by Vector3.new(1,0,1) ) or by setting one Y to equal the other.

Btw once this is working, if it is possible for the boat to rotate, make sure you check that it still works when that happens. There is an extra step required if that is the case.

Left and right, that’s Y axis for orientation.

It works :

rs.Events.Boat.TowersManager.OnServerEvent:Connect(function(player, mousePos)
	local boat = player:FindFirstChild("currentBoat")
	if boat and boat.Value then 
		for _, tower in pairs(boat.Value:GetChildren()) do
			if tower:IsA("Model") and tower.Name:sub(1,5) == "Tower" then
				local turretPart = tower.PrimaryPart
				local pivot = turretPart.Position
				local flatMousePos = Vector3.new(mousePos.X, pivot.Y, mousePos.Z)
				local lookAt = CFrame.lookAt(pivot, flatMousePos)
				local _, y, _ = lookAt:ToEulerAnglesYXZ()
				local newOrientation = CFrame.new(pivot) * CFrame.Angles(0, y, 0)
				turretPart.CFrame = newOrientation
			end
		end
	end
end)

1 Like

yeah, dang, I always forget that, orientation sometimes confuses me.

1 Like

Thanks guys, very helpful ! Have a great day/night !

Be careful as ToEuler/CFrame.Angles is not always reversible if you don’t use all the components.

I have another problem :(.

rs.Events.Boat.TowersManager.OnServerEvent:Connect(function(player, mousePos)
local boat = player:FindFirstChild(“currentBoat”)
if boat and boat.Value then
for _, tower in pairs(boat.Value:GetChildren()) do
if tower:IsA(“Model”) and tower.Name:sub(1,5) == “Tower” then
local turretPart = tower.PrimaryPart
local pivot = turretPart.Position
local flatMousePos = Vector3.new(mousePos.X, pivot.Y, mousePos.Z)
local distance = (flatMousePos - pivot).Magnitude
local minDistance = 20

			if distance >= minDistance then
				local lookAt = CFrame.lookAt(pivot, flatMousePos)
				local _, y, _ = lookAt:ToEulerAnglesYXZ()
				local newOrientation = CFrame.new(pivot) * CFrame.Angles(0, y, 0)
				turretPart:PivotTo(newOrientation)
			end
		end
	end
end

end)

My parts are flying :
image
image

im going to try and troubleshoot this for you by looking at the script and whatnot, but in the meantime, can you edit the message to format the script correctly? Also, @azqjanna, I’m just going to ping you so you are aware of this since you weren’t replied to.
(pet peeve on editing the message, just makes it a bit easier to read)

rs.Events.Boat.TowersManager.OnServerEvent:Connect(function(player, mousePos)
	local boat = player:FindFirstChild("currentBoat")
	if boat and boat.Value then 
		for _, tower in pairs(boat.Value:GetChildren()) do
			if tower:IsA("Model") and tower.Name:sub(1,5) == "Tower" then
				local turretPart = tower.PrimaryPart
				local pivot = turretPart.Position
				local flatMousePos = Vector3.new(mousePos.X, pivot.Y, mousePos.Z)
				local distance = (flatMousePos - pivot).Magnitude
				local minDistance = 20

				if distance >= minDistance then
					local lookAt = CFrame.lookAt(pivot, flatMousePos)
					local _, y, _ = lookAt:ToEulerAnglesYXZ()
					local newOrientation = CFrame.new(pivot) * CFrame.Angles(0, y, 0)
					turretPart:PivotTo(newOrientation)
				end
			end
		end
	end
end)

That’s the code.

yeah, I’m stumped, @azqjanna probably knows more about CFrames than me, but the only thing Id try is:

turretPart:PivotTo(CFrame.lookAt(pivot, flatMousePos))

but I doubt it would work, just really all I could possibly guess, because I’m not really seeing any obvious issues.

also, did you use lookAt:ToEulerAnglesYXZ() instead of lookAt:ToEulerAnglesXYZ() on purpose? Thats all else I see, but I don’t know how that would affect the height of where the turret is placed and make it go higher and higher.

I’m still looking at this but make sure that turretPart is not itself moved by PivotTo, otherwise there will be feedback that can cause what you’ve shown. I.e. it should not be a part of a turret but a separate part attached to the boat.

Tried but nothing changed :(.

rs.Events.Boat.TowersManager.OnServerEvent:Connect(function(player, mousePos)
	local boat = player:FindFirstChild("currentBoat")
	if boat and boat.Value then 
		for _, tower in pairs(boat.Value:GetChildren()) do
			if tower:IsA("Model") and tower.Name:sub(1,5) == "Tower" then
				local turretPart = tower.PrimaryPart
				local pivot = turretPart.Position
				local flatMousePos = Vector3.new(mousePos.X, pivot.Y, mousePos.Z)
				local distance = (flatMousePos - pivot).Magnitude
				local minDistance = 20

				if distance >= minDistance then
					local lookAt = CFrame.lookAt(pivot, flatMousePos)
					local _, y, _ = lookAt:ToEulerAnglesXYZ()
					local newOrientation = CFrame.new(pivot) * CFrame.Angles(0, y, 0)
					turretPart:PivotTo(CFrame.lookAt(pivot, flatMousePos))
				end
			end
		end
	end
end)
rs.Events.Boat.TowersManager.OnServerEvent:Connect(function(player, mousePos)
	local boat = player:FindFirstChild("currentBoat")
	if boat and boat.Value then 
		for _, tower in pairs(boat.Value:GetChildren()) do
			if tower:IsA("Model") and tower.Name:sub(1,5) == "Tower" then
				local modelTower = tower.Tower
				local turretPart = modelTower.PrimaryPart
				
				local pivot = turretPart.Position
				local flatMousePos = Vector3.new(mousePos.X, pivot.Y, mousePos.Z)
				local distance = (flatMousePos - pivot).Magnitude
				local minDistance = 20

				if distance >= minDistance then
					local lookAt = CFrame.lookAt(pivot, flatMousePos)
					local _, y, _ = lookAt:ToEulerAnglesYXZ()
					local newOrientation = CFrame.new(pivot) * CFrame.Angles(0, y + math.rad(90), 0)
					modelTower:PivotTo(newOrientation)
				end
			end
		end
	end
end)

I did this :
image
image

I added a primarypart as horizontal and it works now.
Just I add to put “90” on the left due to something but no idea why.