Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Gun faces camera?

My Friend asked me to fix he's gun. I fixed most of it however the ADS seems to work, wrong. The code works as expected however when the user ADS and looks up the gun looks awful. Here is what it looks like http://i.gyazo.com/008fda010fd5d4c2542653fb3ba9fcd3.mp4

-- Variables for services
local render = game:GetService("RunService").RenderStepped
local contextActionService = game:GetService("ContextActionService")
local userInputService = game:GetService("UserInputService")
local hud = nil  local equiped = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Tool = script.Parent
local UID = script.Parent.Configurations.UID.Value or "NoID"
local lmbDown = false
-- Variables for Module Scripts
local screenSpace = require(Tool:WaitForChild("ScreenSpace"))

local connection
-- Variables for character joints

local neck, shoulder, oldNeckC0, oldShoulderC0 

local mobileShouldTrack = true

local function amISitting(character)
	local t = character.Torso
	for _, part in pairs(t:GetConnectedParts(true)) do
		if part:IsA("Seat") or part:IsA("VehicleSeat") then
			return true
		end
	end
end


local function frame(mousePosition)
	if not mobileShouldTrack then return end
	if player.Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Swimming then
		local torso = player.Character.Torso
		local head = player.Character.Head
		
		local toMouse = (mousePosition - head.Position).unit
		local angle = math.acos(toMouse:Dot(Vector3.new(0,1,0)))
		local arm = player.Character:FindFirstChild("Right Arm")
		local fromArmPos = torso.Position + torso.CFrame:vectorToWorldSpace(Vector3.new(
			torso.Size.X/2 + arm.Size.X/2, torso.Size.Y/2 - arm.Size.Z/2, 0))
		local toMouseArm = ((mousePosition - fromArmPos) * Vector3.new(1,0,1)).unit
		local look = (torso.CFrame.lookVector * Vector3.new(1,0,1)).unit


		local lateralAngle = 0
		

		shoulder.C0 = CFrame.new(1,0.5,0) * CFrame.Angles(math.pi/2 - angle,math.pi/2 + lateralAngle,0)	
		

		if not amISitting(player.Character) then
			torso.CFrame = CFrame.new(torso.Position, torso.Position + (Vector3.new(
				mousePosition.X, torso.Position.Y, mousePosition.Z)-torso.Position).unit)
		else
		
		end	
	end
end

local function pcFrame()
	frame(mouse.Hit.p)
end

local function mobileFrame(touch, processed)
	if not processed then
		local test = screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y, 1)
		local nearPos = game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y, 1))
		nearPos = game.Workspace.CurrentCamera.CoordinateFrame.p - nearPos
		local farPos = screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y,50) 
		farPos = game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(farPos) * -1
		if farPos.magnitude > 900 then
			farPos = farPos.unit * 900
		end
		local ray = Ray.new(nearPos, farPos)
		local part, pos = game.Workspace:FindPartOnRay(ray, player.Character)
		if pos then
			frame(pos)
		end
	end
end

local oldIcon = nil
local zoombreak = false
local function removeAim()
		zoombreak = true
	workspace.CurrentCamera.FieldOfView = 70
		script.Parent.Grip =CFrame.new(0.0100012077, -0.307948351, 0, 0.99973309, -0.0231027305, 0, 0.0231027305, 0.99973309, 0, 0, 0, 1)
	wait(0.2)
	zoombreak = false end



local function equip()
	local torso = script.Parent.Parent.Torso
		neck = torso.Neck	
	oldNeckC0 = neck.C0
	shoulder = torso:FindFirstChild("Right Shoulder")
	oldShoulderC0 = shoulder.C0
	
	
	local torso = player.Character.Torso

	-- Setup joint variables
	

	mouse.Button2Down:connect(function () 		if script.Parent.Parent:FindFirstChild("Humanoid")==nil then else 
	for i = 70,50,-2 do
		
		if zoombreak then zoombreak = false break end
	workspace.CurrentCamera.FieldOfView = i
	script.Parent.Grip = CFrame.new(1.51346564, -0.307948351, 0, 0.99973309, -0.0231027305, 0, 0.0231027305, 0.99973309, 0, 0, 0, 1)
	wait()
	end end
end)

mouse.Button2Up:connect(function ()	if script.Parent.Parent:FindFirstChild("Humanoid")==nil then else 
removeAim()
end

		
end)


	
	oldIcon = mouse.Icon
	mouse.Icon = "http://www.roblox.com/asset/?id=79658449"
	

	if userInputService.TouchEnabled then
		connection = userInputService.TouchMoved:connect(mobileFrame)
	else
		connection = render:connect(pcFrame)
	end

	userInputService.TouchStarted:connect(function(touch, processed)
		mobileShouldTrack = not processed
	end)	
	userInputService.TouchEnded:connect(function(touch, processed)
		mobileShouldTrack = false
	end)
	

	game.ReplicatedStorage["ROBLOX_"..UID.."EquipEvent"]:FireServer()


	
	local function autoFire() 
		while lmbDown do
			wait()
		game.ReplicatedStorage["ROBLOX_"..UID.."FireEvent"]:FireServer(mouse.Hit.p)
	
end	
	end

	mouse.Button1Down:connect(function()
	lmbDown = true
	autoFire()
	end)
		mouse.Button1Up:connect(function()
	lmbDown = false
	
	end)
	local function updateAmmoFromReload()
			wait(script.Parent.Configurations.ReloadTime.Value)
				

			
end
	-- Bind reload event to mobile button and r key
	contextActionService:BindActionToInputTypes("Reload", function() 
		game.ReplicatedStorage["ROBLOX_"..UID.."ReloadEvent"]:FireServer()	


		coroutine.resume(coroutine.create(updateAmmoFromReload)) 
	end, true, "r")
	

end

-- Function to bind to Unequip event
local function unequip() removeAim() equiped = false
	if connection then connection:disconnect() end
	contextActionService:UnbindAction("Reload")
	game.ReplicatedStorage["ROBLOX_"..UID.."UnequipEvent"]:FireServer()
	mouse.Icon = oldIcon


end


-- Bind tool events
Tool.Equipped:connect(equip)
Tool.Unequipped:connect(unequip)

Any Advice on how to point the ADS towards the camera?

This script is in a localscript on a fliteringEnabled Game. (Other users should not see ADS)

1 answer

It requires advance knowledge of programming and math heck I have been continuously learning since last summer at the beginning and I just figured this out when I asked litzo at RDC West and even then it took me 2 more months to comprehend what I saw. You need to use 3 variables the Camera the Arm and the Mouse. You can use the direction part of unitray in mouse or you can use the angular position of the camera and set the arms CFrame to that or you can take the CFrame of the arm and translate to point toward the hit position of your mouse.

3 comments

0

How would I go about doing this?

0

Read it and it tells you not straight out but it explains it

0

I'm not giving you code because I'm on my phone so tl;dr haters

This discussion is preserved as it was. New answers, comments, and votes are closed.