Trying to move block on the x axis only

so when i hold my mouse down, the block will move around the map however i only want the block to be moved on the x axis (left and right) of the block if the mouse is moved left or right if that makes sense. i genuinely have no idea on how to do this so i appreciate any help tysm!!

image

script:

local inputObject = nil
local inputBeganConnection = nil
local inputEndedConnection = nil
local loopConnection = nil
local serverPosition = nil
local raycastParams = RaycastParams.new()

local function rayCast()
	local mouse = uis:GetMouseLocation()
	local ray = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
	local rayResult = workspace:Raycast(ray.Origin, ray.Direction * 100, raycastParams)
	if rayResult == nil then
		return ray.Origin + ray.Direction * 100
	else
		return rayResult.Position, rayResult.Instance
	end
end

local function Loop(deltatime)
	local mouse = uis:GetMouseLocation()
	local position, instance = rayCast()
	
	local yVector = Vector3.new(0,1,0)
	local xVector = Vector3.new(1,0,0)
	
	local CFrameLookAt = CFrame.lookAt(position, c:WaitForChild("HumanoidRootPart").Position)
	local CFrameAngles = CFrame.new():ToEulerAnglesXYZ(0,0,0)
	
	bigCube.CFrame = CFrameLookAt * CFrameAngles

	if (position - serverPosition).Magnitude > 8 then
		serverPosition = position
		PositionEventAgain:FireServer(position)
	end
end

vid: (credits suphi kaner for the script btw)

try this

local pre_position = nil
local function Loop(deltatime)
	local mouse = uis:GetMouseLocation()
	local position, instance = rayCast()

	local yVector = Vector3.new(0,1,0)
	local xVector = Vector3.new(1,0,0)
	if pre_position then
		local delta = position-pre_position
        
		if delta.Magnitude >0  and math.abs(delta.Unit:Dot(xVector)) >0.9 then
			position = Vector3.new(position.X , pre_position.Y,pre_position.Z)
		end
	end
	pre_position = position
	local CFrameLookAt = CFrame.lookAt(position, c:WaitForChild("HumanoidRootPart").Position)
	local CFrameAngles = CFrame.new():ToEulerAnglesXYZ(0,0,0)

	bigCube.CFrame = CFrameLookAt * CFrameAngles

	if (position - serverPosition).Magnitude > 8 then
		serverPosition = position
		PositionEventAgain:FireServer(position)
	end
end
1 Like