How can i keep the model behind the player?

I’m trying to make a model that follows the player from behind and cannot go in front of the player.

Attempt
RunService.RenderStepped:Connect(function()
	local angle = Character.Head.Orientation

	model:SetPrimaryPartCFrame(CFrame.new(Character.Head.Position - Vector3.new(-2,-1,-2)) *CFrame.Angles(0, math.rad(angle.Y), 0))
end)
1 Like

This is where CFrame:ToWorldSpace() comes to play. If you haven’t heard about WorldSpace and ObjectSpace before, I recommend reading up about it on the wiki’s CFrame section.


Basically, what this does is returns a CFrame value from the given CFrame parameter in WorldSpace.

So, we can use this to achieve your request:
(Make sure your Model has a primary part set to it)

local Model = workspace.Model --Set this to the model you want ofc
local Part = game.Players.LocalPlayer.Character.Head --This is the part to go behind. Will be using the player's hide like you did.

local Offset = CFrame.new(0,0,5) --This will be the offset of the position from the Head to the model

game:GetService('RunService').RenderStepped:connect(function()
   local NewModelCFrame = Part.CFrame:ToWorldSpace(Offset) --This will get the CFrame of the position behind the head with the offset we gave it.
   Model:SetPrimaryPartCFrame(NewModelCFrame) --This will put the Model behind the head, 5 studs away.
end)

The above script will also make the model have the same orientation of the head. If you dont want this, just use :PointToWorldSpace() instead of :ToWorldSpace() which returns a Vector3 value instead of a CFrame value.

Hope this helps! :slight_smile:

7 Likes

Thanks a lot, i appreciate it.

1 Like