Need help with Orientations and Positioning with Offset

Hey guys, I need help with trying to get this part be infront of the player.

I have a part, I made its position be with the player’s humanoid root part, and the orientations are the same to.

Script:

while task.wait() do
	workspace.Part.Position = script.Parent:WaitForChild("HumanoidRootPart").Position
	workspace.Part.Orientation = script.Parent:WaitForChild("HumanoidRootPart").Orientation
end

But my question is, how would I make it so that the part is always in front of the player, while also matching up with its Orientation?

Screenshot 2022-10-19 225006
Screenshot 2022-10-19 224730

I’m assuming you want this to be done with CFrame, and not with physics. Just as a heads up, if you are doing a pickup system, I recommend using physics objects instead of the solution I’m about to give.

Anyways, below is a function with a configurable variable that you can use to achieve what you want!

local PartOffsetStuds = 3

local function UpdatePartCFrame(MovedPart, HumanoidRootPart)
	-- Check if humanoid root part is not deleted/parented to nil
	if HumanoidRootPart and HumanoidRootPart.Parent then
		-- We want the part CFrame to be the same as HumanoidRootPart's, plus the x-# of studs in front
		local PartCFrame = HumanoidRootPart.CFrame * CFrame.new(0, 0, -PartOffsetStuds)
		
		-- Set the orientation and position at the same time with CFrame
		MovedPart.CFrame = PartCFrame
	else
		warn("HumanoidRootPart does not exist! Cannot safely read CFrame.")
	end
end

The result looks like this when done every render frame:

Trkv39zL

1 Like

It worked! But could you explain the math? I’m still trying to figure out the difference from the CFrame and Position.

p.s. thanks

1 Like

Position and CFrame are drastically different.

Position is a lot more simple. Positions are stored as a Vector3 values, which work exactly like the vector math you learn in trig/calc. Positions store direction and magnitude, resulting in a 3-part value: x, y, and z.

CFrame is something completely different. Computed as a 4x4 matrix, CFrames store both position and orientation. CFrames are much more powerful when calculating position and orientation related things. Learn more here: CFrames | Roblox Creator Documentation

Side note: CFrames can only be multiplied, and not divided, added, or subtracted. Learn more here: Roblox: CFrames Cheat Sheet by Ozzypig - Download free from Cheatography - Cheatography.com: Cheat Sheets For Every Occasion

1 Like

Could you explain how PartCFrame = HumanoidRootPart.CFrame * CFrame.new(0, 0, -PartOffsetStuds) works?

Yeah! Basically it takes the position and rotation of HumanoidRootPart, and “adds” PartOffsetStuds amount in front of it.

1 Like