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

How would I make a circle - rotating part with CFrame of a part?

Asked by
KDarren12 705 Donator Moderation Voter
5 years ago

I understand that I could have used animation, but I took a couple weeks off of scripting and kind of forgot how to? Anyways, I could use some help using CFrame to make a part move around in a circle (not rotation). If you could edit this code to help me, it would help a lot, thanks.

for i = 1,math.huge do
p.CFrame = plr.Character.Head.CFrame + Vector3.new(i,5,i)
p.Orientation = Vector3.new(i,5,0)
wait(.05)
end

I am adding this to my code for design, i tried a for i = 1,math.huge loop and made it move the X and Z vectors to i every loop. It only fly's off into space. I wanted to try using rotation to make it move forward where the part is facing, but I knew for a fact it wouldn't work.

0
Note that p is a variable for the part I am using, and plr is the targetted player in game:GetService'Players'. KDarren12 705 — 5y
0
ScriptGuider made a good answer for this type of question, it uses the same logic as what I think you're asking. https://scriptinghelpers.org/questions/25441/rotating-around-a-point pidgey 548 — 5y
0
I appreciate the reference, pid! However, I would do it slightly differently these days. I've posted an answer that I believe to be a better version of my old answers on the topic on this post. ScriptGuider 5640 — 5y
0
waitholdonaminute, do you get notified for referencing links, or are u just rllllly active still KDarren12 705 — 5y
0
Just happened to be a coincidence ScriptGuider 5640 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 1 year ago

Whenever you're simulating the movement or animation of something, it should always be a function of time. In other words, "where should this part be t seconds from now?" or "how should this object look t seconds from now?"

This way, instead of basing your animation on a constant "step" or "speed" when you have inconsistent intervals, you base the animation on what the intervals should add up to (the time it takes to complete.) In turn, this does define the speed the animation will run in, while making it a lot smoother.

Here's an example using your situation:

-- time (in seconds) it takes to complete one full rotation
local cycle_duration = 5

while true do
    part.CFrame = part.CFrame
        * CFrame.Angles(0, math.rad((wait() / cycle_duration) * 360), 0)
end

The math can be explained as follows:

  • wait() / cycle_duration

    • The percent of the total cycle duration that elapsed in the wait() interval. Ideally, you'll want to do this locally using RunService.RenderStepped:Wait() or something of sorts.
  • (wait() / cycle_duration) * 360

    • Tells us to take that percent from before, and multiply it by a full rotation (360 degrees) to calculate what the new angle increment should be.
  • math.rad((wait() / cycle_duration) * 360)

    • Convert this angle increment from degrees into radians. This completes the procedure.

Of course, the axis you wish to rotate around and the speed at which you want it to rotate is left for you to easily configure. For faster rotations, lower the cycle_duration to a shorter amount of time. For slower rotations, lengthen it. If you want to rotate around the x-axis, then put the angle increment in CFrame.Angles(x, 0, 0). If you want to rotate around the y-axis, put the angle increment in CFrame.Angles(0, x, 0). You get the idea.

Hope this helped!

Edit

The solution you're looking for isn't all that different from what was provided above, but there are some changes worth noting that I'll cover.

Here is the modified code:

local RunService = game:GetService("RunService")
local rstep = RunService.RenderStepped

-- time (in seconds) it takes to complete one full rotation (this controls speed)
local cycle_duration = 5

-- how many studs the orbiting part is away from it's center
local radius_offset = 10

-- some reference to a part that the other part will orbit around
local center_part = workspace.Part

while true do
    -- creating a variable for the angle increment
    local angle_inc = math.rad((rstep:Wait() / cycle_duration) * 360)

    part.CFrame = center_part.CFrame
        * CFrame.Angles(0, angle_inc, 0)
        * CFrame.new(0, 0, radius_offset)
end

Here is an explanation of the modifications:

  • radius_offset

    • This determines how far away the part is from the object it's orbiting around.
  • center_part

    • This should be the central part that the satellite part is orbiting around.
  • local angle_inc = math.rad((rstep:Wait() / cycle_duration) * 360)

    • This is just a clean-up of the previous code, and also implementing RunService.RenderStepped:Wait() instead of just wait() as mentioned before.
  • CFrame.new(0, 0, radius_offset)

    • This is the CFrame that offsets the satellite part on the Z axis by radius_offset studs. You are also free to modify the X and Y axes to offset the satellite differently as well.

Expected Result

That should cover everything. Again, hope this helps. If this still isn't what you were looking for, let me know and I'll try to clarify.

0
I just want to clarify I'm not trying in any way to be rude in this comment I just wanted to ask if you misunderstood. If I can try to explain this in better detail, I love how this script works, but, I need it to rotate AROUND my head, not just rotate in circles. Sorry for the issue! KDarren12 705 — 5y
0
No disrespect taken. After reading your question again thoroughly, it seems I did slightly misunderstand your question. I'll update my answer soon to suite your real question. ScriptGuider 5640 — 5y
0
Thank you. KDarren12 705 — 5y
Ad
Log in to vote
0
Answered by
mc3334 649 Moderation Voter
5 years ago
Edited 5 years ago

This is the easiest way as far as I know:

while wait() do
    p.CFrame = CFrame.new(plr.Character.Head.CFrame*CFrame.Angles(math.rad(p.Orientation.X+1),math.rad(p.Orientation.Y+1),math.rad(p.Orientation.Z+1)))
end

--That should do the same thing as your script and hopefully work.

0
I get the error of "Vector3 expected, got CFrame" on the line of p.CFrame...? KDarren12 705 — 5y
0
mc3334, You are putting next to no effort into your answers. This code has compilation errors among other things, you aren't even giving any explanation to your code. https://scriptinghelpers.org/blog/posting-good-questions-and-answers-on-scripting-helpers KDarren, look at OP comments, I posted a link to an answer which is intuitive and well-put together. I think it will really help you. pidgey 548 — 5y

Answer this question