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?
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:
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