Problem with getting position behind attachment based on lookvector

  1. What do you want to achieve? Keep it simple and clear!

I want to set the ray cast position to behind the firepoint (an attachment in the viewmodel’s handle). So I can use the raycast to check if the player’s gun is close to a wall, and if it is move it back enough to where it is not gonna be clipping into the wall

  1. What is the issue? Include screenshots / videos if possible!

The math is incorrect, and I do not know the correct math, the calculation causes the positioning to be off from what I intend.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried various different equations, and I am just stumped on how to do this, I thought
    local positionBehind = cframe.Position - (forwardVector * length)
    would be correct but I guess not
  • there are no errors or warnings in output.

most of this code is not nessecary, but is here to give a bigger picture, look for the comment

-- DEVFORUM HELP HERE!! ------------

for where the problem is.

also apologizes for the messy code.


updateCoroutine = coroutine.create(function()
	config.viewArms:PivotTo(game.Workspace.Camera.CFrame)
	local filterList = {config.viewArms, config.viewModel, player.Character}

	raycastParams1.FilterDescendantsInstances = filterList

	game["Run Service"].RenderStepped:Connect(function(dt)		
		if config.viewArms then
			
			if not character then
				player.CharacterAdded:Wait()
			end
			
			local velocity = character:WaitForChild("HumanoidRootPart").Velocity
			local finalOffset = CFrame.new(Vector3.new(-0.65,-0.1,2.125))

			local mouseDelta = game:GetService("UserInputService"):GetMouseDelta()
			local speed = 1
			local modifier = 0.05

			local movementSway = Vector3.new(getBobbing(10,speed,modifier),getBobbing(5,speed,modifier),getBobbing(5,speed,modifier))

			for i,v in pairs(config.viewArms:GetDescendants()) do -- for some reason roblox keeps trying to be silly
				if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
					v.CanCollide = false
				end
			end

			if config.armsOffset then
				finalOffset = config.armsOffset
			end

			springs.sway:shove(Vector3.new(mouseDelta.x / 400,mouseDelta.y / 400)) --not sure if this needs deltaTime filtering
			springs.walkCycle:shove((movementSway / 25) * dt * 60 * velocity.Magnitude)
			
			if didFire == true then -- testing, ignore this, dosent work atm
				print("yes")
				didFire = false
				local testVector3 = Vector3.new(game.Workspace.Camera.CFrame.LookVector.X,game.Workspace.Camera.CFrame.LookVector.Y, game.Workspace.Camera.CFrame.LookVector.Z)
				print(tostring(testVector3))
				springs.sway:shove(testVector3)
			end
			
			local sway = springs.sway:update(dt)
			local walkCycle = springs.walkCycle:update(dt)
			local fireEffect = springs.fireEffect:update(dt)
			
			local newCFrame = CFrame.new()

			config.viewArms.PrimaryPart.CFrame = game.Workspace:WaitForChild("Camera").CFrame:ToWorldSpace(finalOffset)
			config.viewArms.PrimaryPart.CFrame = config.viewArms.PrimaryPart.CFrame:ToWorldSpace(CFrame.new(walkCycle.x / 5,walkCycle.y / 5,0))

			if isHoldingRMB ~= true then
				newCFrame = config.viewArms.PrimaryPart.CFrame * CFrame.Angles(0,-sway.x,sway.y)
				newCFrame = config.viewArms.PrimaryPart.CFrame * CFrame.Angles(0,walkCycle.y,walkCycle.x)
			elseif isHoldingRMB == true then
				local difference = config.viewArms:GetPivot().Position - config.viewModel:WaitForChild("AimPart").Position
				newCFrame = (config.viewArms.PrimaryPart.CFrame * CFrame.Angles(0,-sway.x,sway.y)) + difference
				newCFrame = (config.viewArms.PrimaryPart.CFrame * CFrame.Angles(0,walkCycle.y,walkCycle.x)) + difference 
			end
			
			local sizeVector = config.viewModel:WaitForChild("Body"):GetExtentsSize()
			local length =  math.max(sizeVector.X, sizeVector.Y, sizeVector.Z)
			local filterTable = {}
			table.insert(filterTable, game:GetService("CollectionService"):GetTagged("rayException"))
			table.insert(filterTable, character)
			table.insert(filterTable, config.viewModel)
			table.insert(filterTable, config.viewArms)
			raycastParams1.FilterDescendantsInstances = filterTable
			
			local lookVector = game.Workspace.Camera.CFrame.LookVector
			local part = Instance.new("Part")
			part.Parent = game.Workspace
			part.Size = Vector3.new(0.5, 0.5, 0.5)
			part.Anchored = true
			part.CanCollide = true
			game.Debris:AddItem(part, 0.1)
			
			local rayPosition = config.viewModel.Handle.FirePoint.WorldPosition - (config.viewModel.Handle.FirePoint.WorldCFrame.LookVector * length) -- DEVFORUM HELP HERE!! ------------
			part.Position = rayPosition
			local raycastDirection = config.viewModel.Handle.FirePoint.WorldCFrame.LookVector * length  

			local raycast = workspace:Raycast(rayPosition, raycastDirection, raycastParams1)
			if raycast then
					
				local dist = math.abs(raycast.Distance - length)
				newCFrame = newCFrame + Vector3.new((-(length - dist) * config.viewArms:GetPivot().LookVector.X),(-(length - dist) * config.viewArms:GetPivot().LookVector.Y),(-(length - dist) * config.viewArms:GetPivot().LookVector.Z))
				local part = Instance.new("Part")
				part.Size = Vector3.new(0.5, 0.5, 0.5)
				part.Anchored = true
				part.Parent = workspace
				part.Color = Color3.fromRGB(math.random(50,255), math.random(50,255), math.random(50,255))
				part.Material = Enum.Material.Neon
				part.Position = raycast.Position
				part.CanCollide = false
				part.Transparency = 0.99
				part:AddTag("rayException")
			
				game:GetService("Debris"):AddItem(part, 1)
			end

			config.viewArms.PrimaryPart.CFrame = newCFrame

			-- for arms moving up and down with camera

			local cameraCFrame = workspace.Camera.CFrame
			local lookAt = CFrame.lookAt(character:WaitForChild("Torso").Position, cameraCFrame.Position) -- may need to change origin to each arm

			if character and config.Model:FindFirstChild("server_handler") and config.Model:FindFirstChild("server_handler"):FindFirstChild("Arms") then
				local mouse = player:GetMouse()
				local rightX, rightY, rightZ = character.Torso["Right Shoulder"].C0:ToEulerAnglesYXZ()
				config.Model:FindFirstChild("server_handler"):FindFirstChild("Arms"):FireServer("right", (character.Torso["Right Shoulder"].C0 * CFrame.Angles(0, 0, -rightZ)) * CFrame.Angles(0, 0, math.asin((mouse.Hit.p - mouse.Origin.p).unit.y)))

				local leftX, leftY, leftZ = character.Torso["Left Shoulder"].C0:ToEulerAnglesYXZ()
				character.Torso["Left Shoulder"].C0 = (character.Torso["Left Shoulder"].C0 * CFrame.Angles(0, 0, -leftZ)) * CFrame.Angles(0, 0, math.asin((-mouse.Hit.p - -mouse.Origin.p).unit.y))
				config.Model:FindFirstChild("server_handler"):FindFirstChild("Arms"):FireServer("left", (character.Torso["Left Shoulder"].C0 * CFrame.Angles(0, 0, -leftZ)) * CFrame.Angles(0, 0, math.asin((-mouse.Hit.p - -mouse.Origin.p).unit.y)))
			end

		end
	end)
end)

If you need me to elaborate on something or have any questions relating to helping me find a solution feel free to ask! Any help is greatly appreciated.

marking this as the solution to close this, no longer checking for help

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