How to rotate a part around another part's Y axis?


Pretty self explanatory: I want this part to move in an orbit around this other long part.

I don’t know where to start on doing this as I’m quite bad at math. I’d really appreciate if anyone could give me some guidance.

Ultimately what I’m trying to do is have this shopping cart tilt over when it’s turning as that’s a nice effect. I think if I can figure out how to do the first part I should be able to apply it here.

Thanks in advance for any help!

math.sin() and math.cos() are your best bet

local origin = game.Workspace.main
local radius = 5
local speed = 1 
local angle = 0

while true do
	task.wait()
	angle += speed * 0.05

	local x = math.sin(angle) * radius
	local z = math.cos(angle) * radius
	script.Parent.Position = origin.Position + Vector3.new(x, 0, z)
end
3 Likes

Honestly I don’t even think this approach is needed.

Why should the cart have to rely on another part to handle its own orientation?

Assuming this is a player controlled cart, you first should check for user input. For simplicity, I’m only going to account for PC players in this example. If the player presses “A”, you should just smoothly tween the model on the desired axis. And the same applies for the “S” key.

How are you moving the cart, by CFrame or by Constraints?
It’ll make a difference on how you apply the leaning.

This worked, thanks. However, I’d like it to rotate itself as well like here to achieve that tilting effect:

By changing it to:

	local x = math.sin(angle) * radius
	local y = math.cos(angle) * radius
	script.Parent.Position = origin.Position + Vector3.new(x, y, 0)

I get this:


How can I make the orbiting object rotate itself too like in the first image? I appreciate your help so far.

Well, if I simply rotate the cart along its look vector, I get this:


I’d like it to “tilt” on this axis:
abee86e0b8f37ee73c3f3ab3dff9e843
Hope that explains it :slight_smile:

I’m moving it through CFrame, so the current approach should work.

I can’t test this out since im using my phone and i’ve never done anything like this before so don’t expect it to work

Client side

local Cart = PathHere
local _,YAxis,_ = Cart.CFrame:ToEulerAnglesYXZ()

local Strength = 10
local MaxAngle = 15

game:GetService("RunService").Heartbeat:Connect(function()
        local X, NewYAxis,_= Cart.CFrame:ToEulerAnglesYXZ()
        local YDifference = math.deg(YAxis) - math.deg(NewYAxis)
        local ZAxis = math.clamp(YDifference * Strength, -MaxAngle, MaxAngle)

        Cart.CFrame = CFrame.new(Cart.CFrame.Position) * CFrame.fromEulerAnglesYXZ(X, NewYAxis, math.rad(ZAxis))
        YAxis = NewYAxis
end)

Hey, so I think I got what your looking for, adjust the settings a little to your liking, remove the negative to the rotation if you want it to tilt inwards. Anyway, here you go:

local origin = workspace.Origin
local radius = 5
local speed = 1

local angle = 0
local TiltAngle = 8

while task.wait() do
	angle += speed * 0.05

	local xPos = math.sin(angle) * radius
	local zPos = math.cos(angle) * radius

	local nextAngle = angle + 0.01
	
	local nextZPos = math.cos(nextAngle) * radius
	local nextXPos = math.sin(nextAngle) * radius
	
	local nextPosition = origin.CFrame * Vector3.new(nextXPos, 0, nextZPos)

	script.Parent.CFrame = CFrame.lookAt(origin.CFrame * Vector3.new(xPos, 0, zPos), nextPosition)
		* CFrame.Angles(0, 0, -math.rad(TiltAngle))
end

I used @neviquatro’s base rotation code, and adjusted it to what you wanted and put in the comments.

So thank you @neviquatro.

2 Likes

That worked; thank you all for the help!

while task.wait() do
	angle += speed * 0.05

task.wait() will wait 1 frame which means the part will rotate depending on the fps rate
which can be fixed by u using a time global like tick or time rather than incrementing angle

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.