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

how to make circular motion with objects?

Asked by 3 years ago
Edited 3 years ago

ive been trying to model the solar system, however i cant get a planet orbit another

picture

--[[
_G.mercx is 100, mercz is 0
]]

xris = false
zris = false
while wait(.05) do
    if _G.mercx >= -100 and xris == false then
        _G.mercx = _G.mercx - 1
    elseif _G.mercx <= -100 and xris == false then
        xris = true
    elseif _G.mercx < 100 and xris == true then
        _G.mercx = _G.mercx + 1
    elseif _G.mercx == 100  and xris == true then
        xris = false
    end
    if _G.mercz > -100 and zris == false then
        _G.mercz = _G.mercz - 1
    elseif _G.mercz == -100 and zris == false then
        zris = true
    elseif _G.mercz <= 100 and zris == true then
        _G.mercz = _G.mercz + 1
    elseif _G.mercz > 100  and zris == true then
        zris = false
    end
    script.Parent.Velocity = Vector3.new(_G.mercx, 0, _G.mercz)
end
--merc is short of mercury, that i am trying to model

its rather an oval than a circle, and not even repeating

1 answer

Log in to vote
1
Answered by
TGazza 1336 Moderation Voter
3 years ago

Try this:

local self = script.Parent
local MyMoon = workspace.Moon

--// make these different values to make the moon orbit in an oval path, or keep them the same for a perfect circle path!.
local ampX = 20 
local ampZ = 20
local Speed = 0.1
while true do
    local a = (tick() * Speed) * math.pi
    local x = ampX * math.sin(a)
    local z = ampZ * math.cos(a)
    MyMoon.Position = self.Position+Vector3.new(x, 0 ,z)
    wait()
end

Change the self and MyMoon Variables to match yours. Also Change the Speed and ampX/Z for the orbit speed and radius, Keep ampX/Z the same value for a perfect circle or alter them for a more ellipse or oval path and change the Speed where Higher number here means faster orbit speed.

The obvious problem with this script is you cannot have/detect collisions like for instance a asteroid collides with the moon part;The following script

local Moon = script.Parent
Moon.Touched:Connect(function(otr))
    if(otr.Name == "Asteroid") then
        otr:Destroy()
    end
end)

May not work as desired. To get round this you would calculate the distance of the asteroid with the moon or earth part and if the Asteroid distance Magnitude is less than the radius of the moon or the earth then its hit the planet/moon

Something like:

local Asteroid = workspace.Asteroid
while true do
    local EarthRad = script.Parent.Size.Magnitude
    local AstRad = Asteroid.Size .Magnitude
    local Dist = (Earth.Position - Asteroid.Position).magnitude
    if(Dist < EarthRad) then
    --// Boom! we have a collision!
    end

wait()
end

Hope this helps!

Ad

Answer this question