Part not orbiting properly

  1. What do you want to achieve? Trying to get part to orbit around another / character

  2. What is the issue? Tried several scripts and those that worked the part seems to orbit away from the part that it’s suppose to orbit on

  1. What solutions have you tried so far? This was one of the scripts i got from a post here.
local OrbitPart = script.Parent
local HRP = script.Parent.Parent

local r = 5; --radius is commonly represented as a lowercase r in geometrical calculation
local rps = math.pi; --abbreviation for radians per second. note that pi radians equals 180 degrees

local angle = 0; --beginning angle in radians because radians are awesome
game:GetService'RunService'.Heartbeat:Connect(function(dt) --note that dt is short for deltatime, a value representing the time it took since the last frame for this frame to begin
	angle = (angle + dt * rps) % (2 * math.pi); --add our rotation per second to our total angle after multiplying it by the deltatime to ensure the addition happens over the course of a second (if we didnt multiply this by the deltatime we would essentially be rotating by pi radians approximately every 30th of a second which is extremely fast). we should also ensure our angle value doesnt exceed 2pi radians (equivalent to 360 degrees) through modulus which isnt necessary but i believe to be good practice nonetheless
	OrbitPart.CFrame = HRP.CFrame * CFrame.new(math.cos(angle) * r, 0, math.sin(angle) * r); --set the cframe of the orbiter every frame. our trigonometrical results should be multiplied by our radius to extend the distance according to the value. the way i arranged the trig here causes it to rotate on the y-plane but this can be changed pretty easily.
end)

another script that I tried

local OrbitPart = script.Parent
local HRP = script.Parent.Parent

while task.wait(0.001) do
	task.wait(0.001)
	local angle = math.rad(orbitSpeed * tick()) 
	local offset = Vector3.new(orbitRadius * math.sin(angle), 0, orbitRadius * math.cos(angle)) 

	OrbitPart.Position = HRP.Position + offset
end

Ultimately I’m trying to get it orbit around my player and it does the same thing when it’s attached to HumanoidRootPart

Here is the issue:

  • When you set a new position through absolute means(mathematically in this case), the result often contains decimal issues that usually cause it to shift. Such shifts can be noticeable.
  • This is also true for the other script.
  • Because setting the absolute position of where the orbit would be per frame is prone to floating-point precision errors, you need to reframe the solution to a different method.
  • You can try to change the pivot(an attachment on exactly the same place of the HRP) to rotate itself while the orbital part is attached to it. You’ll only have to rotate the attachment.

Took your advice and it worked! Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.