Problem with car tilting

i have an alignorientation instance in my car to tilt my car when i press keys like W, A, S, D. if im holding W and A or W and D and i release D, it will reset my car rotation back to 0. i want it so that if i’m holding W and D together and i release D, it will only reset my right rotation to 0 and keep the from rotation for W. hopefully that makes sense. thx!!

client:

local function handle_keybinds(inp:InputObject, gameproc)
	if gameproc then return end
	local occupant = zalek_data.seats.control_seat.Occupant
	if inp.UserInputType == Enum.UserInputType.Keyboard then
		if occupant then
			local plr_occ = game.Players:GetPlayerFromCharacter(occupant.Parent)
			if plr_occ then
				if inp.KeyCode == zalek_data.controls.headlights then
					keys_event:FireServer("headlights")
				elseif inp.KeyCode == zalek_data.controls.fly_forward then
					keys_event:FireServer("fly_forward")
				elseif inp.KeyCode == zalek_data.controls.fly_backwards then
					keys_event:FireServer("fly_backwards")
				elseif inp.KeyCode == zalek_data.controls.fly_left then
					keys_event:FireServer("fly_left")
				elseif inp.KeyCode == zalek_data.controls.fly_right then
					keys_event:FireServer("fly_right")
				end
			end
		end
	end
end
uis.InputBegan:Connect(handle_keybinds)

local function handle_keybindsEnded(inp, gameproc)
	if gameproc then return end
	local occupant = zalek_data.seats.control_seat.Occupant
	if inp.UserInputType == Enum.UserInputType.Keyboard then
		if occupant then
			local plr_occ = game.Players:GetPlayerFromCharacter(occupant.Parent)
			if plr_occ then
				if inp.KeyCode == zalek_data.controls.fly_forward then
					keys_event:FireServer("stop_fly_forward")
				elseif inp.KeyCode == zalek_data.controls.fly_backwards then
					keys_event:FireServer("stop_fly_backwards")
				elseif inp.KeyCode == zalek_data.controls.fly_left then
					keys_event:FireServer("stop_fly_left")
				elseif inp.KeyCode == zalek_data.controls.fly_right then
					keys_event:FireServer("stop_fly_right")
				end
			end
		end
	end
end
uis.InputEnded:Connect(handle_keybindsEnded)

server:

local frontbackTilt = math.rad(10)
local leftrightTilt = math.rad(15)
local clampedfrontbackTilt = math.clamp(frontbackTilt, -frontbackTilt, frontbackTilt)
local clampedleftrightTilt = math.clamp(leftrightTilt, -leftrightTilt, leftrightTilt)

local function tiltcar(p, action_name)
	local align_orientation = zalek.car_body.AlignOrientation
	if action_name == "fly_forward" then
		align_orientation.CFrame = align_orientation.CFrame * CFrame.Angles(0,0,-clampedfrontbackTilt)
	elseif action_name == "fly_backwards" then
		align_orientation.CFrame = align_orientation.CFrame * CFrame.Angles(0,0,clampedfrontbackTilt)
	elseif action_name == "fly_left" then
		align_orientation.CFrame = align_orientation.CFrame * CFrame.Angles(-clampedleftrightTilt,0,0)
	elseif action_name == "fly_right" then
		align_orientation.CFrame = align_orientation.CFrame * CFrame.Angles(clampedleftrightTilt,0,0)
	elseif action_name == "stop_fly_forward" then
		align_orientation.CFrame = CFrame.new(0,0,0)
	elseif action_name == "stop_fly_backwards" then
		align_orientation.CFrame = CFrame.new(0,0,0)
	elseif action_name == "stop_fly_left" then
		align_orientation.CFrame = CFrame.new(0,0,0)
	elseif action_name == "stop_fly_right" then
		align_orientation.CFrame = CFrame.new(0,0,0)
	end
end
keys_event.OnServerEvent:Connect(tiltcar)
2 Likes

you can keep track of current till state and instead of resetting the entrie cframe when a key is released just reset the axis you can reset the z axis for forward/backward movement ad left right you can reset only the x axist

local frontbackTilt = math.rad(10)
local leftrightTilt = math.rad(15)
local clampedfrontbackTilt = math.clamp(frontbackTilt, -frontbackTilt, frontbackTilt)
local clampedleftrightTilt = math.clamp(leftrightTilt, -leftrightTilt, leftrightTilt)

local currentTilt = CFrame.new()

local function tiltcar(p, action_name)
    local align_orientation = zalek.car_body.AlignOrientation
    
    if action_name == "fly_forward" then
        currentTilt = currentTilt * CFrame.Angles(0, 0, -clampedfrontbackTilt)
    elseif action_name == "fly_backwards" then
        currentTilt = currentTilt * CFrame.Angles(0, 0, clampedfrontbackTilt)
    elseif action_name == "fly_left" then
        currentTilt = currentTilt * CFrame.Angles(-clampedleftrightTilt, 0, 0)
    elseif action_name == "fly_right" then
        currentTilt = currentTilt * CFrame.Angles(clampedleftrightTilt, 0, 0)
    elseif action_name == "stop_fly_forward" or action_name == "stop_fly_backwards" then
        currentTilt = CFrame.new(currentTilt.X, currentTilt.Y, 0)
    elseif action_name == "stop_fly_left" or action_name == "stop_fly_right" then
        currentTilt = CFrame.new(0, currentTilt.Y, currentTilt.Z)
    end
    
    align_orientation.CFrame = currentTilt
end

keys_event.OnServerEvent:Connect(tiltcar)
3 Likes

hey mate, thanks a million for your help.

with a few tweaks i got a more less desired-result thanks a million man!!!

local currentTilt = CFrame.new()
local frontbackTilt = math.rad(10)
local leftrightTilt = math.rad(15)
local clampedfrontbackTilt = math.clamp(frontbackTilt, -frontbackTilt, frontbackTilt)
local clampedleftrightTilt = math.clamp(leftrightTilt, -leftrightTilt, leftrightTilt)

local function tiltcar(p, action_name)
	local align_orientation = zalek.car_body.AlignOrientation
	local xRot, yRot, zRot = currentTilt:ToEulerAnglesXYZ()

	if action_name == "fly_forward" then
		zRot = zRot - clampedfrontbackTilt
	elseif action_name == "fly_backwards" then
		zRot = zRot + clampedfrontbackTilt
	elseif action_name == "fly_left" then
		xRot = xRot - clampedleftrightTilt
	elseif action_name == "fly_right" then
		xRot = xRot + clampedleftrightTilt
	elseif action_name == "stop_fly_forward" or action_name == "stop_fly_backwards" then
		zRot = 0
	elseif action_name == "stop_fly_left" or action_name == "stop_fly_right" then
		xRot = 0
	end

	currentTilt = CFrame.Angles(xRot, yRot, zRot)
	align_orientation.CFrame = currentTilt
end

keys_event.OnServerEvent:Connect(tiltcar)

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