Model Following cursor inches into camera?

I’m trying to make a model follow the players cursor. The problem I’m having is the model always inches its way back into the camera like so.

robloxapp-20230612-0056330.wmv (1.5 MB)

I made a post yesterday about this topic it went answered but I gave solution before trying said solution. (Help with moving model to players cursor raycast)

Any help or ideas would be greatly appreciated. Thanks in advance.

–Snips of code


local Blacklist = {workspace.CurrentCamera, player.Character.PrimaryPart, workspace.Camera.CameraSubject, workspace.Camera}


local function getMousePosition()
	local unitRay = mouse.UnitRay
	local rayOrigin = unitRay.Origin
	local rayDirection = unitRay.Direction
	local rayLength = 300 -- Adjust this value based on your needs
	
	

	local RayParams = RaycastParams.new()
	RayParams.FilterType = Enum.RaycastFilterType.Exclude
	RayParams.FilterDescendantsInstances = {workspace.Camera, workspace.CurrentCamera}

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection * rayLength, RayParams )


	if raycastResult then
		local x = math.ceil(raycastResult.Position.X / 0.5 - 0.5) * 0.5
		local y = math.ceil(raycastResult.Position.Y / 0.5 - 0.5) * 0.5
		local z = math.ceil(raycastResult.Position.Z / 0.5 - 0.5) * 0.5

		Mouse_Position = raycastResult
		
		
		
		return Vector3.new(x,y,z)
		
	end
	return nil
end






		if script.Stamp_Mode.Value == true then	
			
			local pos = getMousePosition() print(pos)	
			local Temp_Model = nil
			
			--find the only temp model that should b in workspace
			for a, b in ipairs(workspace:GetDescendants()) do
				if b.Name == ("Temporary_Model") and b:IsA("BoolValue") then
					Temp_Model = b.Parent
				end
			end
			
			
			--keep the temp model updated with cursor if found
			if Temp_Model ~= nil then
				
				print(Mouse_Position)
				local Movable_Object = Temp_Model.PrimaryPart.Position
				local formula = pos - Movable_Object
				
				Temp_Model:SetPrimaryPartCFrame(CFrame.new(pos))
				
				
				if table.find(Blacklist,Temp_Model) then 
				else
					table.insert(Blacklist,Temp_Model) 
				end
				

			end
			
			
			
			
			
		end

The reason may be that your raycast from the camera rests on the object and recursively moves it closer to the camera.

The solution may be to add all the building blocks and other things to the FilterDescendantInstances of Raycast with the Exclude filtering type

1 Like

One thing: you can add to FilterDescendantsInstances any Instance containing BasePart types that will be added to the blacklist afterwards

1 Like