Basically, I wrote this code for a tool gun. It works, but it is very buggy and sucks. I am aware that using CFrames are better, but I don’t know how to.
local mouse=game.Players.LocalPlayer:GetMouse()
local sight=script.Parent:WaitForChild("Sight")
local ts = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local head=game.Players.LocalPlayer.Character:WaitForChild("Head")
local mouseaim=false
mouse.Button2Down:Connect(function()
if mouseaim==false then
camera.CameraType = Enum.CameraType.Scriptable
local tween = ts:Create(camera, TweenInfo.new(0.3), {["CFrame"] = sight.CFrame})
tween:Play()
tween.Completed:Connect(function()
camera.CameraType = Enum.CameraType.Scriptable
mouseaim=true
while mouseaim~=false do
wait()
camera.CFrame=sight.CFrame
end
end)
end
end)
mouse.Button2Up:Connect(function()
if mouseaim==true then
camera.CameraType = Enum.CameraType.Scriptable
local tween = ts:Create(camera, TweenInfo.new(0.3), {["CFrame"] = head.CFrame})
tween:Play()
tween.Completed:Connect(function()
camera.CameraType = Enum.CameraType.Custom
mouseaim=false
end)
end
end)
How do I make it less buggy and more efficient?
In what way is it buggy???
The constant while loop makes the camera snap aggressively to the aim part, and when you aim, you can’t turn the camera because the camera mode is set to scriptable. Also, if you spam the right mouse button, the camera detaches from the gun (again, another scriptable camera mode issue)
Okay, I’m gonna test it out and see if I can resolve at least one of the issues and make it more efficient.
1 Like
Here it is:
local mouse=game.Players.LocalPlayer:GetMouse()
local sight=script.Parent:WaitForChild("Sight")
local ts = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local head=game.Players.LocalPlayer.Character:WaitForChild("Head")
local mouseaim=false
local CameraCFrame
mouse.Button2Down:Connect(function()
CameraCFrame = camera.CFrame
camera.CameraType = Enum.CameraType.Scriptable
local tween = ts:Create(camera, TweenInfo.new(0.3), {["CFrame"] = sight.CFrame})
tween:Play()
tween.Completed:Wait()
camera.CameraType = Enum.CameraType.Scriptable
mouseaim=true
camera.CFrame = sight.CFrame
repeat camera.CFrame = sight.CFrame task.wait() until mouseaim == false
end)
mouse.Button2Up:Connect(function()
camera.CameraType = Enum.CameraType.Scriptable
local tween = ts:Create(camera, TweenInfo.new(0.3), {["CFrame"] = CameraCFrame})
tween:Play()
tween.Completed:Wait()
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = CameraCFrame
mouseaim=false
end)
I tried it out and there was no snapping at all so try it out and tell me the result.
edit: Accidently removed a part that I thought was unnecessary so I added it back.
another edit: Still had some minor bugs i will fix that.
2 Likes
I think that you would probably have to just make the gun move instead of making the camera move because making a system that has smooth camera aiming and camera rotation while the camera is in scriptable mode sounds pretty complicated. I might edit this so that it has code that makes the gun move instead of the camera because it would be a whole lot simpler to do it that way.
1 Like
So i’ve been experimenting with the :lerp() function, even though I can’t seem to figure out how to make the function actually work as indented
mouse.Button2Down:Connect(function()
aimCF=sight.CFrame
task.wait()
local offset = head.CFrame
sight.CFrame = aimCF:Lerp(offset,aimSmooth)
end)
mouse.Button2Up:Connect(function()
aimCF=sight.CFrame
task.wait()
local offset = aimCF
sight.CFrame = head.CFrame:Lerp(offset,aimSmooth)
end)
Right now it justs teleports you around a bit
How would I make the gun move instead? Because this is a tool, I can only use animations to move it, but it’s going to be a hassle lining up the aim part with the head every time.
That is sort of what I have been trying to figure out. But I am getting that one bug with my animations that make it fail to load every single time. I am making slight progress though.