Orbit around moving object?

What I am trying to accomplish is to have a part orbit around a player while the player is still able to move, I have seen a couple of threads on creating a script for orbiting, but most of these are based on the fact that the subject is a static object and not moving. I thought of a couple of ways to do this, but I am not sure how to account for the subject’s position change.

I also looked into using LineForces to create the desired effect, but I am not that familiar with how they work, other than the basic usage.

If anyone has an example, or repo of them doing something like this it would be much appreciated!

4 Likes

What you could do to orbit something around a moving object is by doing some simple CFrame manipulation.

All you really need to do is this:

local RunService = game:GetService("RunService")

local PartToOrbit = workspace.Part
local OrbitingPart = workspace.Part2

local RotStep = 0

RunService.Stepped:Connect(function()
   RotStep = RotStep + .1
   
   OrbitingPart.CFrame = CFrame.new(PartToOrbit.Position) * CFrame.Angles(math.rad(RotStep), 0, 0)
end)
3 Likes

This is working, but how would I go about adding an offset to it, since I do not want the orbiting part to be inside of the other part.

1 Like

You would do:

CFrame.new(PartToOrbit.Position) * CFrame.Angles(math.rad(RotStep), 0, 0) * CFrame.new(0, 0, 0)
4 Likes

Ah okay, I originally had it outside the parentheses and it was not producing the desired effect, thank you!

1 Like

Oh yeah, I should probably mention you can do this with welds.

local Weld = workspace.Part.Weld

local RotStep = 0

RunService.Stepped:Connect(function()
   RotStep = RotStep + .1
   
   Weld.C0 = CFrame.new(Weld.C0.Position) * CFrame.Angles(math.rad(RotStep), 0, 0) * CFrame.new(0, 0, 0)
end)
3 Likes