-
What do you want to achieve? Trying to get part to orbit around another / character
-
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
- 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