Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use CFrame to rotate a part with the player?

Asked by 3 years ago
local RunService = game:GetService('RunService')
local Player = game.Players.LocalPlayer
local PlayerName = Player.Name
local Character = game.Workspace:WaitForChild(PlayerName)

local Part = Instance.new("Part")
Part.Parent = game.Workspace
Part.CanCollide = false
Part.Anchored = true

local BodyPosition = Instance.new("BodyPosition")
BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyPosition.P = 1000
BodyPosition.D = 0
BodyPosition.Parent = Part

local BodyOrientation = Instance.new('BodyGyro')
BodyOrientation.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
BodyOrientation.P = 100
BodyOrientation.D = 0
BodyOrientation.Parent = Part

local function onRenderStep(deltaTime)
    local PlayerPosition = Character.HumanoidRootPart.Position
    local PlayerCFrame = Character.HumanoidRootPart.CFrame
    BodyPosition = PlayerPosition
    BodyOrientation.CFrame = PlayerCFrame
    local OverallPosition = Vector3.new(PlayerPosition.x, PlayerPosition.y, PlayerPosition.z +3)
    Part.Position = OverallPosition
    Part.BodyGyro.CFrame = BodyOrientation.CFrame
end
RunService.RenderStepped:Connect(onRenderStep)

The code for the part to follow works as intended, I've tried multiple times to rotate the part with the player, but I cant seem to find out what to use for it. Any help would be appreciated.

0
You can try using AlignOrientation JustinWe12 723 — 3y

1 answer

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

There were a few things I noticed. For each thing I added or changed, I included a comment (*new*). Anything from your original code that I removed, I left but commented out.

local RunService = game:GetService('RunService')
local Player = game.Players.LocalPlayer
local PlayerName = Player.Name
local Character = Player.Character -- *new*
repeat -- *new*
    Character = Player.Character -- *new*
    wait -- *new*
until Character ~= nil -- *new*

local Part = Instance.new("Part")
Part.Parent = game.Workspace
Part.CanCollide = false
Part.Anchored = false -- *new*

local BodyPosition = Instance.new("BodyPosition")
BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyPosition.P = 1000
BodyPosition.D = 100
BodyPosition.Parent = Part

local BodyOrientation = Instance.new('BodyGyro')
BodyOrientation.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
BodyOrientation.P = 100
BodyOrientation.D = 10
BodyOrientation.Parent = Part

local BodyForce = Instance.new("BodyForce",Part) -- *new*
BodyForce.Force = Vector3.new(0,workspace.Gravity * Part:GetMass(), 0) -- *new*

local function onRenderStep(deltaTime)
    local PlayerPosition = Character.HumanoidRootPart.Position
    local PlayerCFrame = Character.HumanoidRootPart.CFrame

    --BodyPosition.Position = PlayerPosition
    BodyOrientation.CFrame = PlayerCFrame

    --local OverallPosition = Vector3.new(PlayerPosition.x, PlayerPosition.y, PlayerPosition.z +3)

    local Distance = 10 -- *new*
    local OverallPosition = PlayerPosition - (PlayerCFrame.LookVector * Distance) -- *new*


    --Part.Position = OverallPosition
    -- If you are using BodyPosition, you don't need to adjust the part's position like this

    BodyPosition.Position = OverallPosition -- *new*

    Part.BodyGyro.CFrame = BodyOrientation.CFrame
end
RunService.RenderStepped:Connect(onRenderStep)

The BodyForce object I added negates the effects of gravity, allowing the part to be non-anchored.

This line keep the position of the part behind the player:

local OverallPosition = PlayerPosition - (PlayerCFrame.LookVector * Distance)

The BodyPosition instance sets the part's position to the position of the BodyPosition. You don't have to set the part's position and the BodyPosition's position, only the BodyPosition's position.

Dampeners are also a good idea for BodyGyro and BodyPosition, unless you want so freaky effects to occur ( I assume you don't ).

This line will change how far behind the player the part is positioned:

local Distance = 10 -- *new*

If you would like further assistance with your code, or would like something explained in a more detailed / different manner, feel free to contact me on Discord (phxntxsmic#2021)

0
Thank you very much! The only problem is when you changed my WaitForChild for PlayerName, it exhausted the script, other than that, works as intended, thank you. Danjonu 0 — 3y
1
Yeah, I fixed the script exhaustion. If it fixes your problem, please be sure to accept the answer so we can both gain some reputation. appxritixn 2235 — 3y
Ad

Answer this question