I’m creating a player controlled turrent and I’m trying to do the up and down movement of the turrent, I’ve already solved the 360 degree turning and just need to fix the up and down movement.
As you can see the turrent moves normally however when you go up or down then it looks weird. I’m trying to limit the up and down movement to the barrel of the turrent only instead of the whole turrent.
Here is my code:
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
Mouse.TargetFilter = Turrent
TurrentBody.WeldConstraint.Enabled = false
TurrentBody.CFrame = CFrame.new(TurrentBody.Position, Mouse.Hit.Position)
TurrentBody.WeldConstraint.Enabled = true
end
end)
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
Mouse.TargetFilter = Turrent
local _,y,_ = CFrame.new(TurrentBody.Position, Mouse.Hit.Position):ToOrientation()
TurrentBody.WeldConstraint.Enabled = false
TurrentBody.CFrame = CFrame.new(TurrentBody.Position)*CFrame.Angles(0,y,0) -- 90 degree max rotation
TurrentBody.WeldConstraint.Enabled = true
end
end)
Explaination
What I did here is I broke the CFrame Orientation into its x,y,z componenets using CFrame:ToOrientation() which returns a tuple so you can use it like local X,Y,Z = CFrane:ToOrientation() where CFrame being your CFrame.
Thanks, after tweaking around with it a bit it works however how would I make it so when you move the mouse up the barrel of the turrent moves up and when you move it down it goes back to its original position?
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
Mouse.TargetFilter = Turrent
local x,y,_ = CFrame.new(TurrentBody.Position, Mouse.Hit.Position):ToOrientation() -- x is the up down axis of the mouse. Now just set the barrels cframe to x using a weld constraint for simplistics sake.
TurrentBody.WeldConstraint.Enabled = false
TurrentBody.CFrame = CFrame.new(TurrentBody.Position)*CFrame.Angles(0,y,0) -- 90 degree max rotation
TurrentBody.WeldConstraint.Enabled = true
end
end)