Help with moving model to players cursor raycast

I have a UI that clones a model locally and the model will follow the cursor. This works fine but the only problem I have is that the model gets stuck on the players camera somehow and when I move it off it always just inches back to the front of the camera. [See Below]

robloxapp-20230612-0056330.wmv (1.5 MB)

I would like to know if there are any steps I’m missing or things I’m not accounting for in my method.
Thanks for any help in advance!

		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 raycastResult = workspace:Raycast(rayOrigin, rayDirection * rayLength)

			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 = Vector3.new(x,y,z)
				return Vector3.new(x,y,z)
			end
			return nil
		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 = Mouse_Position - Movable_Object
				Temp_Model:TranslateBy(formula)
			end

See what happens when you turn CanCollide off

1 Like

You need to add a blacklist to the raycast parameters:

local Blacklist = {game.Camera}

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Greylist
RayParams.FilterDescendantsInstances = {Blacklist}

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

1 Like

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