LookVector arms colliding with invisible parts

I am using a script that I found in this dev forum post that makes your arms move with your camera, it seemed to be working as expected at first but then I realized that in can collide with transparent parts. Does anyone know how to prevent this from happening or make it not collide with them at all?

Local Script:

local replicated = game:GetService("ReplicatedStorage")
local remote = replicated:WaitForChild("UpdateArms")
local run = game:GetService("RunService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character	
local humanoid = character:WaitForChild("Humanoid")
local root = character:WaitForChild("HumanoidRootPart")
local head = character:WaitForChild("Head")
local torso, rightArm, neck, rightShoulder
if humanoid.RigType == Enum.HumanoidRigType.R6 then
	torso = character:WaitForChild("Torso")
	rightArm = character:WaitForChild("Right Arm")
	neck = torso:WaitForChild("Neck")
	rightShoulder = rightArm:WaitForChild("Right Shoulder")
elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
	torso = character:WaitForChild("UpperTorso")
	rightArm = character:WaitForChild("RightUpperArm")
	neck = head:WaitForChild("Neck")
	rightShoulder = rightArm:WaitForChild("RightShoulder")
end

local function onRenderStep()
	local state = humanoid:GetState()
	if not state then return end
	if state == Enum.HumanoidStateType.Swimming then return end
	local mousePosition = mouse.Hit.Position
	local toMouse = (mousePosition - head.Position).Unit
	local angle = math.acos(toMouse:Dot(Vector3.new(0, 1, 0)))
	local neckAngle = angle

	if math.deg(neckAngle) > 110 then
		neckAngle = math.rad(110)
	end

	if humanoid.RigType == Enum.HumanoidRigType.R6 then
		neck.C0 = CFrame.new(0, 1, 0) * CFrame.Angles(math.pi - neckAngle, math.pi, 0)
	elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
		neck.C0 = CFrame.new(0, 0.84, 0) * CFrame.Angles(math.pi/2 - neckAngle, 0, 0)
	end

	local fromArmPos = torso.Position + torso.CFrame:vectorToWorldSpace(Vector3.new(torso.Size.X/2 + rightArm.Size.X/2, torso.Size.Y/2 - rightArm.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 = math.acos(toMouseArm:Dot(look))

	if tostring(lateralAngle) == "-1.#IND" then
		lateralAngle = 0
	end

	if state == Enum.HumanoidStateType.Seated then
		local cross = root.CFrame.lookVector:Cross(toMouseArm)
		if lateralAngle > math.pi/2 then
			lateralAngle = math.pi/2
		end
		if cross.Y < 0 then
			lateralAngle = -lateralAngle
		end
	end	

	if humanoid.RigType == Enum.HumanoidRigType.R6 then
		rightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(math.pi/2 - angle, math.pi/2 + lateralAngle, 0)
	elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
		rightShoulder.C0 = CFrame.new(1.25, 0.5, 0) * CFrame.Angles(math.pi/2 - angle, lateralAngle, 0)
	end

	if state ~= Enum.HumanoidStateType.Seated then
		root.CFrame = CFrame.lookAt(root.Position, root.Position + (Vector3.new(mousePosition.X, root.Position.Y, mousePosition.Z) - root.Position).Unit)
	end

	remote:FireServer(neck.C0, rightShoulder.C0)
end

run.RenderStepped:Connect(onRenderStep)

Server Script:

local replicated = game:GetService("ReplicatedStorage")
local remote = replicated.UpdateArms

local function onRemoteFired(player, neckC0, rightShoulderC0)
	local character = player.Character
	if not character then return end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	if humanoid.Health <= 0 then return end
	if humanoid.RigType == Enum.HumanoidRigType.R6 then
		local torso = character.Torso
		local neck = torso.Neck
		local rightShoulder = torso["Right Shoulder"]
		neck.C0 = neckC0
		rightShoulder.CO = rightShoulderC0
	elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
		local head = character.Head
		local neck = head.Neck
		local rightArm = character.RightUpperArm
		local rightShoulder = rightArm.RightShoulder
		neck.C0 = neckC0
		rightShoulder.C0 = rightShoulder.C0
	end
end

remote.OnServerEvent:Connect(onRemoteFired)

you can use PhysicsService. prevents a block from coming into contact with a block
ex:

local ps = game:GetService("PhysicsService")

ps:CreateCollisionGroup("Part1")
ps:CreateCollisionGroup("Part2")

workspace:WaitForChild("Part1")
workspace:WaitForChild("Part2")

ps:SetPartCollisionGroup(workspace.Part1,"Part1")
ps:SetPartCollisionGroup(workspace.Part2,"Part2")

ps:CollisionGroupSetCollidable("Part1","Part2",false)


The part you set must be BasePart
You can set CreateCollisionGroup() and CollisionGroupSetCollidable() only from server
SetPartCollisionGroup() can be set in local

2 Likes

Didn’t think about setting colission groups, I’ll try this.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.