A gun that is based on where you are looking

How to script a gun that looks dependant on where am i looking directly at but and not always forward?

3 Likes

You can use the forward direction of the camera.

1 Like

Sorry for no reply I was busy and it should look like this:



2 Likes

You’re probably gonna have to use some sort of inverse kinematics making a Motor6D on the gun’s handle rotate it towards a part positioned at the player’s mouse position

1 Like

Pretty easy!

  1. Get the Right Shoulder and Left Shoulder Motor6D inside the character’s torso
    Weld | Documentation - Roblox Creator Hub
  2. In a PostSimulation (Or Heartbeat) loop add a variable, and that’s gonna be:
local ToSpace=HumanoidRootPart.CFrame:ToObjectSpace(Mouse.Hit)
  1. Now Lerp the Right and Left Shoulder:
local LookVector=ToSpace.LookVector
local Y=LookVector.Y
local X=LookVector.X
RightShoulder.C0=RightShoulder.C0:Lerp(CFrame.Angles(Y,0,X),DeltaTime--[[From the heartbeat connection]]*10)
LeftShoulder.C0=LeftShoulder.C0:Lerp(CFrame.Angles(Y,0,X),DeltaTime--[[From the heartbeat connection]]*10)

And you should be done!

1 Like

(I’m gonna answer for Red since I wasn’t able to post and I asked him to do it) Would it be possible to animate it or in some way change it to work with the animating? (I don’t quite know how to word this really)

Also, does it move arms only up and down? Since I need them to not just go up and down.

Yes, it also moves left and right.

I’ll test it later (presumably tomorrow) and answer if it works or not, thanks for responding though.

I’m not good at programming something new, more of changing something, so I’d like some correction, is this correct or am I a dummy?

script.Parent.Equipped:Connect(function()
	local LS = script.Parent.Parent:FindFirstChild("Torso")["Left Shoulder"]
	local RS = script.Parent.Parent:FindFirstChild("Torso")["Right Shoulder"]
	local HRP = script.Parent.Parent:FindFirstChild("HumanoidRootPart")
	local Mouse = game.Players.LocalPlayer:GetMouse()
	local ToSpace
	
	coroutine.wrap(function()
		game:GetService("RunService").Heartbeat:Connect(function()
			ToSpace = HRP.CFrame:ToObjectSpace(Mouse.Hit)
			print(ToSpace)
		end)
	end)()
	
	local LookVector = ToSpace.LookVector
	local Y = LookVector.Y
	local X = LookVector.X
	RS.C0 = RS.C0:Lerp(CFrame.Angles(Y, 0, X), ToSpace*10)
	LS.C0 = LS.C0:Lerp(CFrame.Angles(Y, 0, X), ToSpace*10)
end)

I don’t know why would you put heartbeat in a separate thread? connections such as .Heartbeat does not yield unless you use :Wait(); also this would cause a memory leak because youre connecting new heartbeat functions on equipped without cleaning them up

2 Likes

Haven’t I said that I’m not good at programming? I’m sorry if I am being rude but this community just doesn’t help most of the time, like you, all you did was hint at something. Additionally you’re a programmer and not helping with the actual end code.

if you got offended, i am deeply sorry; i was just providing feedback and my concern on the memory leak

sure, I did not provide the fixed end code but I assume that (sorry if this is offensive) you can fix it yourself since I’m also dealing with other threads as well

hope this clarifies :pray:

2 Likes

Could you give even the smallest amount of code to help me, or the threads you are dealing with need very big lines/help?

1 Like

sure, if you insist; here’s the code:

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
local Mouse: Mouse = Player:GetMouse()

local HumanoidRootPart = Character.HumanoidRootPart

local IsEquipping: boolean

script.Parent.Equipped:Connect(function()
	IsEquipping = true
end)

script.Parent.Unequipped:Connect(function()
	IsEquipping = false
end)

-- Use Mouse.Move to avoid computation lags
Mouse.Move:Connect(function()
	if not IsEquipping then
		return
	end
	
	local ToSpace = HumanoidRootPart.CFrame:ToObjectSpace(Mouse.Hit)
	local LS = Character.Torso["Left Shoulder"]
	local RS = Character.Torso["Right Shoulder"]

	local LookVector = ToSpace.LookVector
	local Y = LookVector.Y
	local X = LookVector.X

	RS.C0 = RS.C0:Lerp(CFrame.Angles(Y, 0, X), ToSpace*10)
	LS.C0 = LS.C0:Lerp(CFrame.Angles(Y, 0, X), ToSpace*10)
end)

note that this is untested but in theory should work

1 Like

I’m sorry if I sounded pretty insisting, I was just asking if you could give me some code to work with.

1 Like

It seems like since ToSpace is a CFrame and not Vector3 (additionally being multiplied I guess) the code just poops itself and I don’t know how to really fix it.
image

The script does work since it doesn’t print the error until I equip the tool.

a CFrame cannot be multiplied by a number

try this instead:

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
local Mouse: Mouse = Player:GetMouse()

local HumanoidRootPart = Character.HumanoidRootPart

local IsEquipping: boolean

script.Parent.Equipped:Connect(function()
	IsEquipping = true
end)

script.Parent.Unequipped:Connect(function()
	IsEquipping = false
end)

-- Use Mouse.Move to avoid computation lags
Mouse.Move:Connect(function()
	if not IsEquipping then
		return
	end
	
	local ToSpace: CFrame = HumanoidRootPart.CFrame:ToObjectSpace(Mouse.Hit)
	local LS = Character.Torso["Left Shoulder"]
	local RS = Character.Torso["Right Shoulder"]

	local LookVector = ToSpace.LookVector
	local Y = LookVector.Y
	local X = LookVector.X
	
	local ScaledToSpace = ToSpace * Vector3.one * 10

	RS.C0 = RS.C0:Lerp(CFrame.Angles(Y, 0, X), ScaledToSpace)
	LS.C0 = LS.C0:Lerp(CFrame.Angles(Y, 0, X), ScaledToSpace)
end)

i just realized the second argument of :Lerp needs to be a number

1 Like

“Unable to cast Vector3 to float”

ok i modified it to input the delta time instead

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
local Mouse: Mouse = Player:GetMouse()

local HumanoidRootPart = Character.HumanoidRootPart

local IsEquipping: boolean

script.Parent.Equipped:Connect(function()
	IsEquipping = true
end)

script.Parent.Unequipped:Connect(function()
	IsEquipping = false
end)

game:GetService('RunService'):Connect(function(dt)
	if not IsEquipping then
		return
	end
		
	--local ToSpace: CFrame = HumanoidRootPart.CFrame:ToObjectSpace(Mouse.Hit)
	local LS = Character.Torso["Left Shoulder"]
	local RS = Character.Torso["Right Shoulder"]

	local LookVector = ToSpace.LookVector
	local Y = LookVector.Y
	local X = LookVector.X
	
	--local ScaledToSpace = ToSpace * Vector3.one * 10
	
	RS.C0 = RS.C0:Lerp(CFrame.Angles(Y, 0, X), dt)
	LS.C0 = LS.C0:Lerp(CFrame.Angles(Y, 0, X), dt)
end)
1 Like