Keeping a part in a set position behind me

Hi all, I have been trying to figure this out for hours. I am trying to get a part so that no matter what direction my player is facing the part will always be behind me and to the left. I have got this so far as my closest attempt:

local v = Vector3.new(hcf.x +(-5*look.x),hcf.y,hcf.z + (-5*look.z) )

hcf is the cframe of my humanoidrootpart and look is the lookvector of that cframe.
This manages to keep the part directly behind me however I am unsure on how to move it to the left or right.

1 Like

I believe you want something like this-

Part.wmv (810.1 KB)

You could just do

-- put this server script into StarterPlayer.StarterCharacterScripts
local part = game.Workspace.Part

local character = script.Parent
local head = character:WaitForChild("Head")

part.CFrame = head.CFrame + Vector3.new(0, 0, 5)

local weldConstraint = Instance.new("WeldConstraint")
weldConstraint.Part0 = head
weldConstraint.Part1 = part
weldConstraint.Parent = part

So what this does is basically it sets the CFrame of the part to behind the head.

Then it welds the part with the head so that it moves only when the head moves.

3 Likes

hmm i want the part to fall behind a bit that’s the thing. I currently have it so that the part walks towards the position v. Creating a weld will have a constant attachment at a set distance. I want the distance they part can be from the body to be infinite but they go to that location behind and to the right of the player.

I’m not too sure what you want, but I think you want this -

Part behind.wmv (1.2 MB)

So this is the script

-- put this serverScript into game.StarterPlayer.StarterCharacterScripts
local tweenService = game:GetService("TweenService")

local part = game.Workspace.Part

local character = script.Parent
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local moveBehindTween

local prevHumanoidRootPartPos = humanoidRootPart.Position

local timeToReachDest
local easingStyle = Enum.EasingStyle.Sine
local easingDirection = Enum.EasingDirection.InOut
local noOfTimesToRepeat = 0
local reverse = false
local delayForStart = 0

while wait(0.1) do
	local humanoidRootPartPos = humanoidRootPart.Position
	local humanoidRootPartOrientation = humanoidRootPart.CFrame - humanoidRootPart.Position
	
	if humanoidRootPart == prevHumanoidRootPartPos then
		timeToReachDest = 0.5
		
		if moveBehindTween then
			moveBehindTween:Cancel()
		end
		
		local tweenInfo = TweenInfo.new(timeToReachDest, easingStyle, easingDirection, noOfTimesToRepeat, reverse, delayForStart)
		
		local targets = {
			CFrame = humanoidRootPart.CFrame + humanoidRootPartOrientation:VectorToWorldSpace(Vector3.new(-5, 0, 0))
		}
		
		moveBehindTween = tweenService:Create(part, tweenInfo, targets)
		
		moveBehindTween:Play()
		
	else	
		timeToReachDest = 0.25
		prevHumanoidRootPartPos = humanoidRootPart
		
		if moveBehindTween then
			moveBehindTween:Cancel()
		end
		
		local tweenInfo = TweenInfo.new(timeToReachDest, easingStyle, easingDirection, noOfTimesToRepeat, reverse, delayForStart)
		
		local targets = {
			CFrame = humanoidRootPart.CFrame + humanoidRootPartOrientation:VectorToWorldSpace(Vector3.new(0, 0, 5))
		}
		
		moveBehindTween = tweenService:Create(part, tweenInfo, targets)
		
		moveBehindTween:Play()
	end
	
	
end

Now it is a bit long script, but I will explain it

First of all, it uses tweening for a smooth transition. If you don’t know what tweening is, I suggest checking it out

So what this does it just every 0.1 seconds, it checks the humanoidRootPart’s position
it also has a variable called previousHumanoidRootPartPosition.
So the script just checks if the humanoids position has changed or not

If it has changed, then it sets the target CFrame to
humanoidRootPart’s Position + Vector3.new(0, 0, -5) (5 studs behind the humanoidRootPart’s Position)
So tweening comes into action and changes the position smoothly in 2.5 seconds

But if the position hasn’t changed then it just sets the target frame to
humanoidRootPart’s Position + Vector3.new(-5, 0, 0) (5 studs beside the humanoidRootPart’s Position)
And the tweening smoothly transitions it from its position to 5 studs beside the humanoidRootPart

And that’s basically it and I hope I helped.

CFrame means your Position + Direction so if you * A CFrame With another its like vector but face you everytime

local Part = workspace.Part -- Your Part

local Player = game.Players.LocalPlayer -- Player
local Character = Player.Character -- character

repeat wait() until game:IsLoaded() -- waiting for game to load and start

local RootPart = Character.HumanoidRootPart -- Getting humanoidrootpart

game:GetService("Run Service").RenderStepped:Connect(function()
      Part.CFrame = HumanoidRootPart.CFrame * CFrame.new(0,0,5) -- the * CFrame.new() is important so it follow ur face
end)
7 Likes