How do I make it so the part follows the mouse upwards in certain distance too?

I wanted to make it so the part could follow the mouse in every direction, even upwards but only within certain distance

local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local humrp = char:WaitForChild("HumanoidRootPart")

local camera = workspace.CurrentCamera

local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace

local params = RaycastParams.new()
params.FilterDescendantsInstances = {char, part}
params.FilterType = Enum.RaycastFilterType.Blacklist

RunService.Heartbeat:Connect(function(delta)
	local mouse = UIS:GetMouseLocation()
	local ray = camera:ViewportPointToRay(mouse.X,mouse.Y)
	local result = workspace:Raycast(ray.Origin,ray.Direction * 50, params)

	if result then
		part.CFrame = CFrame.new(result.Position)
	else
		print("not detected")
	end

end)

You mean like this tutorial?

I believe you can just replace mouseHit with your raycast hit position.

If the raycast didnt hit anything the end position is the ray origin plus the ray direction.

character.Head.Position + (mouse.Hit.Position - character.Head.Position).Unit * 20
1 Like

Thank you sooo much! It works great

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