I would like to get aiming inside of this script to work correctly, but the script ends up breaking or not working, I cannot figure out why the attached script refuses to aim in. I have looked around quite a bit, to try to resolve said issue.
(This is supposed to be a gun engine. The code provided is a client required module.)
local RS = game:GetService('RunService')
local UIS = game:GetService('UserInputService')
local plrs = game.Players
local plr = plrs.LocalPlayer
local cam = workspace.CurrentCamera
local mouse = plr:GetMouse()
local models = game.ReplicatedStorage["BGS Replicated"].vpModels
local mb2DOWN = false
local mb1DOWN = false
local connection = nil
local equipped = false
local module = {}
local function positionViewModel(g, conf)
local gunOffset = conf.offset
local lastcamcf = cam.CFrame
local targetcf = CFrame.new()
local swayOffset = CFrame.new()
local swaySize = 1
local walking = false
local camPart = Instance.new('Part',cam)
camPart.Size = Vector3.new(1,1,1)
camPart.Anchored = true
--camPart.Transparency = 1
camPart.CanCollide = false
local char = plr.Character or plr.CharacterAdded:Wait()
if char then
local hum = char:FindFirstChildWhichIsA('Humanoid')
if hum then
local shirt = char:FindFirstChildWhichIsA('Shirt')
local bodyColors = char:FindFirstChildWhichIsA('BodyColors')
shirt:Clone().Parent = g.ArmModel
bodyColors:Clone().Parent = g.ArmModel
connection = RS.RenderStepped:Connect(function(dt)
camPart.CFrame = cam.CFrame
for i,v in pairs(g:GetDescendants()) do
if v:IsA('BasePart') then
v.CanCollide = false
end
end
if hum.MoveDirection.Magnitude > 0.1 then
walking = true
else
walking = false
end
local mainCamPart = g.GUN.POINTS:FindFirstChild("MAINCAM")
--local camPart = g.GUN.POINTS:FindFirstChild("CAM")
local aimPart = g.GUN.POINTS:FindFirstChild("AIM")
local aimOffset = aimPart.CFrame:Inverse() * camPart.CFrame --* CFrame.new(-0.2473459243774414,0,0)
--aimOffset = aimOffset:Inverse()
if not mainCamPart or not aimPart then
warn("MAINCAM or AIM part not found in POINTS")
return
end
--[[if mb2DOWN then
aimPart.CFrame = cam.CFrame
else
mainCamPart.CFrame = cam.CFrame
end]]
local walkSin = math.sin(time() * 2 * 4) / 25
local walkCos = math.cos(time() * 3 * 4) / 20
local walkSinRot = math.sin(time() * 2 * 4) / 25
local walkCosRot = math.sin(time() * 3 * 4) / 20
local x, y, z = (cam.CFrame:ToObjectSpace(lastcamcf)):ToOrientation()
swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x / 2) * swaySize, math.sin(y / 2) * swaySize, 0), .1)
local walkCFOffset = CFrame.new(walkSin, walkCos, 0) * CFrame.fromEulerAnglesXYZ(0, 0, walkSinRot)
if mb2DOWN then
walkCFOffset = CFrame.new(walkSin / 10, walkCos / 10, 0)
end
if walking and not mb2DOWN then
targetcf = targetcf:Lerp(gunOffset * walkCFOffset * swayOffset, 0.1)
elseif not walking and not mb2DOWN then
targetcf = targetcf:Lerp(gunOffset * swayOffset, 0.1)
end
if walking and mb2DOWN then
targetcf = targetcf:Lerp(walkCFOffset * swayOffset * aimOffset, 0.1)
elseif not walking and mb2DOWN then
targetcf = targetcf:Lerp(swayOffset * aimOffset, 0.1)
end
lastcamcf = cam.CFrame
if mb2DOWN then
print(aimOffset.Position)
mainCamPart:PivotTo(cam.CFrame*targetcf)
else
mainCamPart:PivotTo(cam.CFrame*targetcf)
end
--[[if mb2DOWN then
g:PivotTo(targetcf * aimPart.CFrame:ToObjectSpace(mainCamPart.CFrame))--g:PivotTo(aimPart.CFrame * targetcf * aimPart.CFrame:ToObjectSpace(mainCamPart.CFrame))
else
g:PivotTo( targetcf)--g:PivotTo(mainCamPart.CFrame * targetcf)
end]]
end)
end
end
end
function module.Equip(gName)
local gun = models:FindFirstChild(gName)
mouse.Icon = ' '
if not gun then
return
end
local gun = gun:Clone()
gun.Parent = cam
game.ReplicatedStorage["BGS Replicated"].Remotes["equip tool"]:FireServer()
plr.CameraMode = Enum.CameraMode.LockFirstPerson
equipped = true
positionViewModel(gun, require(gun.conf))
while equipped do
task.wait()
if mb1DOWN then
game.ReplicatedStorage["BGS Replicated"].Remotes["fire tool"]:FireServer(gun.GUN.POINTS.SHOOT.CFrame)
end
end
end
function module.Unequip()
connection:Disconnect()
mouse.Icon = ''
for i,v in pairs(cam:GetChildren()) do
v:Destroy()
end
equipped = false
plr.CameraMode = Enum.CameraMode.Classic
end
UIS.InputBegan:Connect(function(k, gpe)
if gpe then
return
end
if k.UserInputType == Enum.UserInputType.MouseButton2 then
mb2DOWN = true
end
if k.UserInputType == Enum.UserInputType.MouseButton1 then
mb1DOWN = true
end
end)
UIS.InputEnded:Connect(function(k, gpe)
if gpe then
return
end
if k.UserInputType == Enum.UserInputType.MouseButton2 then
mb2DOWN = false
end
if k.UserInputType == Enum.UserInputType.MouseButton1 then
mb1DOWN = false
end
end)
return module
I am attempting to get this line working:
local aimOffset = aimPart.CFrame:Inverse() * camPart.CFrame
(It kinda works? It just moves around 1/2 of the way towards the camera.)
Either way, if you can help, that would be great!