How to create a part that follows the player's position

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to achieve a part that follows the player so my isometric game can work
  2. What is the issue? Include screenshots / videos if possible!
    The ray that i have sent is hitting a set part, named Baseplate, and the higher you go, the worse it is to control the character
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to manually code it in, but it didn’t work.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Script that will be amended to fit this functionality

--//Variables//--
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = game.Workspace.CurrentCamera
local abs = math.abs
local Controller = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
local RunService = game:GetService("RunService")
function getInputDirection()
	local direction = Controller:GetMoveVector()
	return if abs(direction.X) > 0.001 or abs(direction.Z) > 0.001 then Vector3.new(direction.X, 0, direction.Z).Unit else Vector3.zero
end
Camera.CameraType = Enum.CameraType.Custom

--//Main Script//--
RunService:BindToRenderStep("MoveOverride", Enum.RenderPriority.Character.Value + 1, function()
	if Player.Character then
		Camera.FieldOfView = FieldOfView
		local humanoid = Player.Character:FindFirstChild("Humanoid")
		local hrp = Player.Character:FindFirstChild("HumanoidRootPart")
		if humanoid and hrp then
			--//Humanoid Movement.//--
			local characterCFrame = hrp.CFrame
			local fixedDirection = Vector3.zero
			local inputDirection = getInputDirection()
			if inputDirection ~= Vector3.zero then
				fixedDirection = characterCFrame:VectorToWorldSpace(inputDirection)
			end
			humanoid:Move(fixedDirection, false)
			
			--//Isometric Camera//--
			game:GetService("SoundService"):SetListener(Enum.ListenerType.ObjectCFrame, Character.HumanoidRootPart)
			Camera.CFrame = CFrame.new(Vector3.new(Character.HumanoidRootPart.Position.X + zoom, Character.HumanoidRootPart.Position.Y + zoom, Character.HumanoidRootPart.Position.Z + zoom), Character.HumanoidRootPart.Position)
			
			--//Player Look//--
			local mouseray = Mouse.UnitRay
			local raycastparams = RaycastParams.new()
			raycastparams.FilterDescendantsInstances = {workspace.Baseplate}
			raycastparams.FilterType = Enum.RaycastFilterType.Include
			local ray = workspace:Raycast(mouseray.Origin,mouseray.Direction.Unit * 1000, raycastparams)
			if ray then
				local raypos = ray.Position
				local hrpypos = hrp.Position.Y
				hrp.CFrame = CFrame.lookAt(hrp.Position,Vector3.new(raypos.X,hrpypos,raypos.Z))
			end
		end
	end
end)
1 Like

What’s the problem, the part or the camera?

I don’t think I can understand what your issue is. What is the “ray” you mention, why would it hit the Baseplate, why is that an issue, and how does it somehow make it harder to contorl the character??

Answering the title, you can weld the part to the player, or use a loop of your choice to set the CFrame of the part to that of the player.

create a part, then put a body mover in it (BodyPosition). Make a run service script which as it runs, it sets the bodyposition value to the character’s position. You could run complex math and tweenservice to try and make it smooth but body movers are just going to be simpler and better

Here’s a potential script:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local cam = workspace.CurrentCamera

-- Create the camera part
local cameraPart = Instance.new("Part")
cameraPart.Position = character.HumanoidRootPart.Position
cameraPart.Size = Vector3.new(1, 1, 1)
cameraPart.Anchored = false
cameraPart.CanCollide = false
cameraPart.Transparency = 1 -- Make it invisible
cameraPart.Parent = workspace

-- Create BodyPosition for smooth movement
local bodyPosition = Instance.new("BodyPosition")
bodyPosition.MaxForce = Vector3.new(1, 1, 1) * math.huge
bodyPosition.P = 380 -- High value for quick adjustment
bodyPosition.D = 100 -- Damping value to smooth out the motion
bodyPosition.Parent = cameraPart

RunService.RenderStepped:Connect(function()
	local headPosition = character.Head.Position
	local cameraOffset = Vector3.new(0, 2, -6) -- Adjust this as needed for the camera's position relative to the character

	-- Update BodyPosition target position
	bodyPosition.Position = headPosition + Vector3.new(0,3.75,0) + character.Head.CFrame.LookVector * -10

	-- Update cameraPart CFrame to look at the character's head
	local lookAtCFrame = CFrame.new(cameraPart.Position, headPosition)
	cameraPart.CFrame = lookAtCFrame

	-- Set the camera's CFrame to match the cameraPart's CFrame
	cam.CFrame = cameraPart.CFrame
end)