Slight rotation occurring and don't know why

So I am making a game where you control a ball by moving the world around it like Super Monkey Ball however when I spam all of the keys this slight rotation is occurring and its making me mad.


I tried adding it so two tweens can’t go at once but that still didn’t fix it I am confused on why and how this is happening at all.

For more context this is taking place inside of a local script.

local CAS = game:GetService("ContextActionService")
local TS = game:GetService("TweenService")
local base = workspace:WaitForChild("Base")
local StartCFrame = base.CFrame
local Orientation = base.CFrame

local turn = 10
local Active = false

local function Control(actionName, inputState, inputObject)
	local num
	if inputState == Enum.UserInputState.Begin then
		num = 1
	elseif inputState == Enum.UserInputState.End then
		num = -1
	else
		return
	end
	
	if actionName == "Left" then
		Orientation = Orientation*CFrame.Angles(math.rad(turn*num),0,0)
	elseif actionName == "Right" then
		Orientation = Orientation*CFrame.Angles(math.rad(-turn*num),0,0)
	elseif actionName == "Up" then
		Orientation = Orientation*CFrame.Angles(0,0,math.rad(turn*num))
	elseif actionName == "Down" then
		Orientation = Orientation*CFrame.Angles(0,0,math.rad(-turn*num))
	end
	
	if Active then wait() end
	local tween = TS:Create(base, TweenInfo.new(0.2), {CFrame = Orientation})
	Active = true
	tween:Play()
	tween.Completed:Wait()
	Active = false
end

CAS:BindAction("Left", Control, false, Enum.KeyCode.A)
CAS:BindAction("Right", Control, false, Enum.KeyCode.D)
CAS:BindAction("Up", Control, false, Enum.KeyCode.W)
CAS:BindAction("Down", Control, false, Enum.KeyCode.S)

I love the way you logically went through this! I personally struggle with ContextActionService. Anyway the issue here is that you want to set the CFrame of the base to the direction you pressed therefore after checking the key pressed in the Control function you should set the CFrame of the base directly like so:
‘’’
base.CFrame = StartCFrame*CFrame.Angles(the math based on the checked input)
‘’’
This ensures that the base CFrame will only be adjusted relative to its original starting CFrame rather than relative to its current CFrame. That is why the base turns from its current CFrame instead of its original. It should look like the base rolls around from its center point. Don’t forget to mark this post as a solution if it works! :slight_smile:

2 Likes