A gun that is based on where you are looking

I have added the .Heartbeat since you have forgotten it, but it still gives out an error “attempt to index nil with LookVector

oh yeah, i accidentally commented the ToSpace variable:

remove the --

1 Like

That has done it, but now there’s a bug, my hands go behind my back and are rotated like 90 degrees sideways, it seems as how the previous person who have replied has given buggy code, or something is not right (atleast for me).

How would I also make it faster, as how having it delayed would make it much worse than a more realistic approach (which I am doing)?

You could add a 90 or -90 degree offset to the final CFrame with:

	RS.C0 = RS.C0:Lerp(CFrame.Angles(Y, 90, X), dt) -- or -90
    -- do the same with LS.C0
1 Like

you can use .RenderStepped instead of .Heartbeat to make it smoother

1 Like

I have never seen such a more sensetive code, inputting a “5” would make it rotate like 90 degrees, I’m not even sure if it spins it 5 times or not, additionally .RenderStepped does make it faster but it’s still delayed, like 2 sec behind I believe.

Also how would I be able to quote, is it the blockquote feature?

1 Like

Even after centering them back (which felt like ages because it’s pretty buggy), the hands are inside my torso…

you can use the quote feature by highlighting the text and clicking the “Quote” button

im not sure how to fix that but it may be caused by the dt variable (try removing the lerp altogether and directly set the C0 CFrame)

1 Like

This was indeed the problem, replacing it with like 0.2 made it very fast.

But where would I start with this?

Update:

I have fixed the code, made it how I want, but now I have ran into a problem, it ignores every part. To give more information, no matter how close I am to a part, the arms dont move.

The code looks something like this now:

wait()

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').RenderStepped: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 X = LookVector.X
	local Y = LookVector.Y
	local L = LS.C1.Position
	local R = RS.C1.Position

	LS.C1 = LS.C1:Lerp(CFrame.new(L.X, L.Y, L.Z) * CFrame.fromOrientation(-X, math.rad(-90), Y), 0.25)
	RS.C1 = RS.C1:Lerp(CFrame.new(R.X, R.Y, R.Z) * CFrame.fromOrientation(X, math.rad(90), -Y), 0.25)
end)
1 Like

Something seems very off in this code. Why are you putting the contents of a unit vector into fromOrientation? Instead, you should use CFrame.lookAt and make it relative to the torso using ToObjectSpace. The first argument should be the position of the torso, the second being Mouse.Hit.Position.

Also, why are you trying to make Mouse.Hit relative? I’ve never seen anything like it.

1 Like

You should ask the previous repliers about half of the stuff that you referenced, the other half, I’m not a programmer, I used those before and it worked, so I’m fine with them.

Also could you give some code snippets so I could work with them or explain a little more so I could code it myself?

I made a system that used these in the past, and here’s what I did:

local rightWorld = cam.CFrame:VectorToWorldSpace(AssignedOffset)
		local leftWorldOffset = cam.CFrame:VectorToWorldSpace(LeftTargetOffset)
		local leftWorld = cam.CFrame:VectorToWorldSpace(LeftAssignedOffset)
		--Right arm
		local toolOffset = tool:GetAttribute("RightGripOffset")
		if toolOffset then
			rightWorld += tool.Handle.CFrame:VectorToWorldSpace(toolOffset)
		end
		--local offsetWorld = cam.CFrame:VectorToWorldSpace(Vector3.new(-offsetmindist, 0, 0))
		local RightArmCFrame = CFrame.lookAt(cam.CFrame.Position + rightWorld, to--[[ + (offsetWorld * to.Magnitude)]], cam.CFrame.UpVector)
		--[[lRightArmSpring.s = (tool:GetAttribute("RightArmWeight") or .7) * 60
		RightArmSpring.d = (tool:GetAttribute('RightArmSpringDamp') or 1)
		RightArmSpring.t = RightArmCFrame.LookVector
		RightArmSpring.p = RightArmSpring.p.Unit]]
		local RightArmWorldCFrameLerped = CFrame.fromMatrix(RightArmCFrame.Position, --[[RightArmSpring.p]] RightArmCFrame.LookVector:Cross(RightArmCFrame.UpVector), camcf.YVector, --[[-RightArmSpring.p]]camcf.ZVector) * angles:Inverse()
		RightArmWorldCFrame = RightArmWorldCFrameLerped
		C0 = torso.CFrame:ToObjectSpace(RightArmWorldCFrameLerped) * CFrame.Angles(math.rad(90),0,0)
		--dbg.CFrame = cam.CFrame
		--C0 = torso.CFrame:ToObjectSpace(cam.CFrame)
		--C0 = cam.CFrame:ToObjectSpace(CFrame.lookAt(torso.Position + rightWorld, to))
		--Lerp the torso CFrame to the HumanoidRootPart's CFrame to increase arm stability
		arm.C0 = C0

Essentially, the fromMatrix part is the CFrame that the arm attempts to travel to in world space. This is converted into object space using torso.CFrame:ToObjectSpace. Then, the arm’s C0 is set to that CFrame.

Keep in mind that my code is for an FPS game, so you might have to do some adjustments to it. You’ll want to replace the Camera CFrame with a CFrame.lookAt from the head’s position to the mouse position.

1 Like

What is even better, use the C1 which I’m guessing is the actual arm, or the C0 which is the torso (since that’s how it’s labeled)?

Our gun engine (mine and Red’s) will also be used for mostly FPS games so I don’t think I need to adjust anything, maybe slightly.

Well, you might actually want to use Transform in your case.

1 Like