local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(keyCode)
if keyCode.keyCode == Enum.KeyCode.F then
local angle = Mouse.Hit.LookVector
game.ReplicatedStorage.tankTurretEvent:FireServer(angle)
end
end)
The server script:
local turret = script.Parent
local turretMain = turret.TurretMain
local gunMain = turret.GunMain
local turretWeld = turretMain.Weld
local gunWeld = gunMain.Weld
game.ReplicatedStorage.tankTurretEvent.OnServerEvent:Connect(function(player, angle)
turretWeld.C0 = turretWeld.C0 * CFrame.Angles(0,0,angle.X)
end)
local Base = turretWeld.C0
game.ReplicatedStorage.tankTurretEvent.OnServerEvent:Connect(function(player, angle)
turretWeld.C0 = Base * CFrame.Angles(0,0,angle.X)
end)
Lookvector doesn’t give you the angle, it only gives you a vector that’s used to multiply with another vector for an increment in the XYZ relative to that CFrame which is an alternative to multiplying 2 CFrames.
I see that the script is using lookvector, will it work better if you are in 1st person and looking from the point of the barrel? Also I advise creating a loop that will slowly add or subtract the tank rotation towards the mouse lookVector Until it is looking in the same direction (for realisim). BTW, I think that aiming the mouse into nothing might be problematic.
I just realized the problem with these is the camera position, if the camera is not looking from the point of view of the barrel, then it might be off, so it wont work in 3rd person very well.
What you could try is making a new ray pointing in the direction of the mouse.Hit.p or mouse postion in 3D space. The problem with this is rays can only point from one direction. The origin is the start of the ray (first property), then to get the direction of the ray (the second property) then make the client script fire to the server the mouse.Hit.p then use the mouse.Hit.p and subtract it by the ray origin (the front of the tank.) to find the ray direction. Note you will need a part at the front of the tank to define the ray origin.
something like this:
local origin = --part at the front of the tank.
game.ReplicatedStorage.tankTurretEvent.OnServerEvent:Connect(function(player, postion)
local lookvector = position - origin
turretWeld.C0 = turretWeld.C0 * CFrame.Angles(0,0,lookvector.X)
end)
Note: sorry for the late response, it took a bit of digging to get to this conclustion.