Alright getting straight to the point, I’ve been working on an aiming system for tools, whenever you hold right click your character would move in the direction of the camera.
My main issues with the system are:
- Aiming at another player will make your character lock on them
- Aiming down will make your character spin
Video:
My code:
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Core = Character:WaitForChild("HumanoidRootPart")
-----------------
--//VARIABLES//--
-----------------
local Aiming = false
local Mouse = Player:GetMouse()
----------------
--//SERVICES//--
----------------
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
-----------------
--//FUNCTIONS//--
-----------------
function ChangeCameraOffset(Offset)
if not Character then Character = Player.Character or Player.CharacterAdded:Wait() end
local Humanoid = Character:WaitForChild("Humanoid")
local Tween = TweenService:Create(Humanoid, TweenInfo.new(0.6), {CameraOffset = Offset})
Tween:Play()
end
game:GetService("RunService").RenderStepped:Connect(function()
if Character:FindFirstChildOfClass("Tool") then
script.ToolEquipped.Value = true
else
script.ToolEquipped.Value = false
end
if script.ToolEquipped.Value == true and Aiming == true then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
else
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end)
UserInputService.InputBegan:Connect(function(detect, key)
if detect.UserInputType == Enum.UserInputType.MouseButton2 then
if script.ToolEquipped.Value == true then
Aiming = true
Player.CameraMaxZoomDistance = 5
Player.CameraMinZoomDistance = 5
ChangeCameraOffset(Vector3.new(2, 0, -2))
return
end
else
return
end
end)
UserInputService.InputEnded:Connect(function(detect2, key2)
if detect2.UserInputType == Enum.UserInputType.MouseButton2 then
Aiming = false
Player.CameraMaxZoomDistance = 30
Player.CameraMinZoomDistance = 4
ChangeCameraOffset(Vector3.new(0, 0, 0))
end
end)
spawn(function()
while wait() do
if Aiming == true then
local direction = (Mouse.Hit.p - Character.HumanoidRootPart.Position) * Vector3.new(1, 0, 1)
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.Position + direction)
end
end
end)