i used a planet orbitting script from a devfoum resources and editting to have eccentricty and orbital plane but there is a problem with my script which i dont know how to fix
The module script
local RunService = game:GetService("RunService")
local CosineMath = math.cos
local SineMath = math.sin
local Atan2Math = math.atan2
local PIMath = math.pi
local TAU = 2*PIMath
local TweenS = game:GetService('TweenService')
return function(PlanetOrbiting : PVInstance, PlanetGettingOrbited : PVInstance,Distance: number, TimeToOrbit : number, Eccentricity : number, OrbitalPlane : number)
assert(PlanetOrbiting ~= PlanetGettingOrbited, "Cannot orbit itself, PlanetOrbiting and PlanetGettingOrbited should be different")
local DifferenceVector = PlanetOrbiting:GetPivot().Position-PlanetGettingOrbited:GetPivot().Position --Differnce
local Angle = Atan2Math(DifferenceVector.Y, DifferenceVector.X)--Angle
local HeartbeatConnection
HeartbeatConnection = RunService.Heartbeat:Connect(function(DeltaTime)
-- Disconnect the event if one of the planets got destroyed
if not (PlanetOrbiting or PlanetGettingOrbited) then
HeartbeatConnection:Disconnect()
HeartbeatConnection = nil
end
-- Polar coordinates 2D
local x
local y
local z = 0
local offset
if Eccentricity < 0 then
x = (Distance+-Eccentricity)*CosineMath(Angle)
y = Distance*SineMath(Angle)
offset = CFrame.new(Eccentricity/2,0,0)*CFrame.Angles(math.rad(OrbitalPlane)+math.rad(90),0,0)
elseif Eccentricity == 0 then
x = (Distance)*CosineMath(Angle)
y = Distance*SineMath(Angle)
offset = CFrame.new(Eccentricity,0,0)*CFrame.Angles(math.rad(OrbitalPlane)+math.rad(90),0,0)
elseif Eccentricity > 0 then
x = (Distance+Eccentricity)*CosineMath(Angle)
y = Distance*SineMath(Angle)
offset = CFrame.new(Eccentricity/2,0,0)*CFrame.Angles(math.rad(OrbitalPlane)+math.rad(90),0,0)
end
PlanetOrbiting:PivotTo((CFrame.new(PlanetGettingOrbited.Position)*offset)*CFrame.new(x, y, z))
Angle += DeltaTime*TAU/TimeToOrbit
end)
--HeartbeatConnection:Disconnect()
-- Return the heartbeat connection, so we can disconnect it if we no longer wants the part to orbit
return HeartbeatConnection
end
so if you put eccentricty as 15 it would look like this
But the thing with this is that the planet is moving at a constant speed but in real life planets move faster as they are closer to their parent object like this
this is how my code works in game
the speed is same on all places which isnt what i want
Any idea on how to create that?
Thanks!