I didn’t learn this from any source, only 2 years of prototyping my thoughts into code and progressively learning here on the dev forum on what habits and ways I need to write code out. If you’re beginner, I took an entire week about 3 years ago learning the basics from Peaspod with some prior basic experience from GameMaker Studio, Unity, and Java. With this project, I wanted to learn more about CFrame equations and mathematics.
Here’s what I did to make that system. It’s pretty simple, one thing to know is that planet is moving around a star, and the star is moving around a black hole with other star systems with there own planets moving around them. This poses a problem with ordinary loops, so we need to update every frame prior by using RenderStepped.
For every frame, the part position needs to be updated so
Part.CFrame = CFrame.new(Mouse.Hit.p)
.
Then I need it to face the planet’s center, so I have a part to represent the center of the planet and is welded to the planet. Part.CFrame = CFrame.new(Part.Position, HomePlanet.Center.Position)
.
After that, I need to face the bottom of the part to the planet’s center by using Part.CFrame = Part.CFrame:ToWorldSpace(CFrame.Angles(math.rad(90),math.rad(Rotation),0))
.
The math.rad(90)
makes the part flip so the bottom is facing towards the center of the planet and the Rotation is rotating the part of that position. To actually clone and place it is mostly everything I said above but I need to apply a weld constraint and set some values for other scripts to use.
--Update
Mouse.TargetFilter = Part
local Update = RunService.RenderStepped:Connect(function()
if Mouse.Target == HomePlanet then
Part.Parent = HomePlanet.PlanetSurface
Part.CFrame = CFrame.new(Mouse.Hit.p)
Part.CFrame = CFrame.new(Part.Position, HomePlanet.Center.Position)
Part.CFrame = Part.CFrame:ToWorldSpace(CFrame.Angles(math.rad(90),math.rad(Rotation),0))
else
Part.Parent = game.ReplicatedStorage
end
end)
I think my coding skills are basic to intermediate, for any other programmers reading this, is there a better way of writing code out, or is this the way to write code?