(sorry for my english i’m using a translator, some things might not be clear, just ask me and i clarify them)
Hello, i’m working on a first person gun without fake arms, the camera works pretty well but the gun doesn’t point towards camera, i searched a bit in devforum and found this:
local WeldOffset = HumanoidRootPart.CFrame:Inverse() * Torso.CFrame
local weld = Instance.new("Weld")
weld.Part0 = HumanoidRootPart
weld.Part1 = Torso
weld.Parent = Character
RunService.Heartbeat:Connect( function()
local ReferenceCFrame = CFrame.new(HumanoidRootPart.Position, Mouse.Hit.p)
weld.C0 = WeldOffset * HumanoidRootPart.CFrame:toObjectSpace(ReferenceCFrame)
end)
This worked, but this script uses (position, position), my gun uses raycast, which uses (position, direction) so sometimes the gun gets too close from the camera (depending of the distance of the mouse hit p):
What i want to achieve is instead of using mouse to change the “orientation” of the root part i want to use the direction that camera is facing
Gotcha, from what I’m assuming you’re using the Players’ Mouse, you can use Mouse.TargetFilter to ignore any other parts you want the Mouse to look at apart from the Camera itself, which should position the Gun to it’s middle position
Put those Parts (Or walls) inside a Folder that’ll ignore all the descendants of those objects inside that Folder, and I think it should work:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.TargetFilter = workspace.IgnoreParts --This will be your Folder
local WeldOffset = HumanoidRootPart.CFrame:Inverse() * Torso.CFrame
local weld = Instance.new("Weld")
weld.Part0 = HumanoidRootPart
weld.Part1 = Torso
weld.Parent = Character
RunService.Heartbeat:Connect( function()
local ReferenceCFrame = CFrame.new(HumanoidRootPart.Position, Mouse.Hit.p)
weld.C0 = WeldOffset * HumanoidRootPart.CFrame:toObjectSpace(ReferenceCFrame)
end)
Oh you almost made it, sadly, the ray that my gun casts uses mouse.hit.p so if i filter these parts in the folder they also are ignored by the ray because the target filter is set in the player mouse, not just inside the script.
edit: if you don’t understood, that’s the line of code that casts the ray:
local Raycast = game.Workspace:Raycast(FirePart.Position, CFrame.new(FirePart.Position, MouseHitp).LookVector * 100, Params)
local RunService = game:GetService("RunService")
local Uis = game:GetService("UserInputService")
local RemoteEvent = game.ReplicatedStorage.ChangeRootCFrame
local Event = game.ReplicatedStorage.ChangeCameraCFrame
local Player = script.Parent.Parent
local Character = script.Parent.Parent.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Torso = Character:WaitForChild("Torso")
local Head = Character:WaitForChild("Head")
local RightArm = Character:WaitForChild("Right Arm")
local Mouse = Player:GetMouse()
local Camera = game.Workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable
local default_CameraInstance = Head
local default_CameraRotationX = 0
local default_CameraRotationY = 0
local default_CameraZoom = 1
local cameraInstance = default_CameraInstance
local cameraPosition
local cameraRotationX = default_CameraRotationX
local cameraRotationY = default_CameraRotationY
local cameraZoom = default_CameraZoom
local cameraMouseRotateSpeed = 0.25
-- Camera functions --
RunService.RenderStepped:Connect( function()
for _, v in pairs(Character:GetChildren()) do -- Makes arms visible in first camera
if v:IsA("Part") and v.Name ~= "Right Arm" and v.Name ~= "Left Arm" then
v.LocalTransparencyModifier = 1
elseif v.Name == "Right Arm" or v.Name == "Left Arm" then
v.LocalTransparencyModifier = 0
end
end
if Uis.MouseBehavior ~= Enum.MouseBehavior.LockCenter then -- Locks mouse
Uis.MouseBehavior = Enum.MouseBehavior.LockCenter
end
cameraPosition = cameraInstance.Position
local cameraRotationCFrame = CFrame.Angles(0, cameraRotationX, 0) * CFrame.Angles(cameraRotationY, 0, 0)
Camera.CFrame = cameraRotationCFrame + cameraPosition + cameraRotationCFrame * Vector3.new(0, 0, cameraZoom)
end)
Event.Event:Connect( function(NeedChange, AimPart)
if NeedChange then
cameraInstance = AimPart
cameraZoom = 0
else
cameraInstance = default_CameraInstance
cameraZoom = default_CameraZoom
end
end)
local function UpdateCamera()
local Delta = Uis:GetMouseDelta()
cameraRotationX = cameraRotationX - Delta.X * math.rad(cameraMouseRotateSpeed)
if (cameraRotationY - Delta.Y * math.rad(cameraMouseRotateSpeed)) < 1.3 and (cameraRotationY - Delta.Y * math.rad(cameraMouseRotateSpeed)) > -1.5 then
cameraRotationY = cameraRotationY - Delta.Y * math.rad(cameraMouseRotateSpeed)
end
end
RunService:BindToRenderStep("MouseMovement", Enum.RenderPriority.Input.Value, UpdateCamera)
-- Root part to mouse functions --
local WeldOffset = HumanoidRootPart.CFrame:Inverse() * Torso.CFrame
local weld = Instance.new("Weld")
weld.Part0 = HumanoidRootPart
weld.Part1 = Torso
weld.Parent = Character
RunService.Heartbeat:Connect( function()
local ReferenceCFrame = CFrame.new(HumanoidRootPart.Position, Mouse.Hit.p)
weld.C0 = WeldOffset * HumanoidRootPart.CFrame:toObjectSpace(ReferenceCFrame)
end)
Note: I never use “Sciptable” because then you gotta continually move the camera by yourself…
For this I think that I liked where, “Attach” placed the Camera, but how “Track” followed the bird…
mouse.Button1Down:connect(function()
local chr=sp.Parent
if chr and lasttrigger.Value+cooldown<tick() then
local t=chr:FindFirstChild("Head")
local h=chr:FindFirstChild("Humanoid")
if t and h and h.Health>0 then
if check then
check=false
remoteEvent:FireServer(t, mouse.Hit.p)
lasttrigger.Value=tick()
mouse.Icon="rbxasset://textures\\GunWaitCursor.png"
sp.Handle.Transparency=1
SaveType = workspace.CurrentCamera.CameraType
SaveSubject = workspace.CurrentCamera.CameraSubject
SaveFOV = workspace.CurrentCamera.FieldOfView
SaveCFrame = workspace.CurrentCamera.CFrame
workspace.CurrentCamera.CameraType = "Attach"
workspace.CurrentCamera.CameraSubject = workspace.Crow.Eyes
workspace.CurrentCamera.FieldOfView = 100
wait(.1)
workspace.CurrentCamera.CameraType = "Track"
else
check = true
workspace.CurrentCamera.CameraType = SaveType
workspace.CurrentCamera.CameraSubject = SaveSubject
workspace.CurrentCamera.FieldOfView = SaveFOV
workspace.CurrentCamera.CFrame = SaveCFrame
end
end
end
end)
I tried what you said a time ago but i don’t know why but this makes the character slide through baseplate when i move the camera, forgot to put the solution i found here:
local WeldOffset = HumanoidRootPart.CFrame:Inverse() * Torso.CFrame
local Weld = Instance.new("Weld")
Weld.Part0 = HumanoidRootPart
Weld.Part1 = Torso
Weld.Parent = Character
local Attachment = Instance.new("Attachment")
Attachment.Parent = game.Workspace.Terrain
RunService.Heartbeat:Connect( function()
local ReferenceCFrame = CFrame.new(HumanoidRootPart.Position, Attachment.WorldPosition)
Weld.C0 = WeldOffset * HumanoidRootPart.CFrame:toObjectSpace(ReferenceCFrame)
Attachment.Position = Mouse.Hit.LookVector * 10000
end)
I got this solution in another topics i made, i’ll put it here if someone find this one.