Getting strange/confusing values from a touch's position

I was testing out some mobile input stuff like .TouchStarted and .TouchMoved and while using it I was getting some confusing values. My goal was to get the screen point of the touch into a UDim2.

Video showing issue: (wouldn’t let me upload to post)
video

The UI frames position is the Camera:WorldToScreenPoint function giving the touch’s position in the parameters but converted into a UDim2, if I am doing that correctly.

The Red part’s position is what I get from a Camera:WorldToScreenPoint function giving the touch’s position in the parameters.

The Blue part’s position is the .Position of the touch returned by the event.

local input = game:GetService("UserInputService")

local frame = script.Parent.Frame

input.TouchStarted:Connect(function(touch, gameEvent)
	print("started")
	print(tostring(touch))
	print(touch.Position)
	local newpos = workspace.CurrentCamera:WorldToScreenPoint(touch.Position)
	--print(tostring(touchInfo[name].Position) .. "|" .. tostring(touch.Position))
	print(tostring(Vector2.new(newpos.X, newpos.Y)))
	frame.Position = UDim2.new(0, newpos.X, 0, newpos.Y)
	local newpart = Instance.new("Part")
	newpart.Parent = workspace
	newpart.Anchored = true
	newpart.Material = Enum.Material.Neon
	newpart.BrickColor = BrickColor.Red()
	newpart.Position = touch.Position
	
	local newpart2 = Instance.new("Part")
	newpart2.Parent = workspace
	newpart2.Anchored = true
	newpart2.Material = Enum.Material.Neon
	newpart2.BrickColor = BrickColor.Blue()
	newpart2.Position = newpos
end)

input.TouchMoved:Connect(function(touch, gameEvent)
	print("moved")
	print(tostring(touch))
	print(touch.Position)
	
	local newpos = workspace.CurrentCamera:WorldToScreenPoint(touch.Position)
	print(tostring(Vector2.new(newpos.X, newpos.Y)))
	frame.Position = UDim2.new(0, newpos.X, 0, newpos.Y)
end)

input.TouchEnded:Connect(function(touch, gameEvent)
	print("ended")
	print(tostring(touch))
	print(touch.Position)
end)

If someone could explain what these values are that I am getting, or how to convert them into a UDim2, that’d be very helpful!

1 Like

Found the issue! the touch.Position was actually a Vector3, but the X and Y values are in pixels on the screen. Its still confusing as to what there is a Z value, I’d think that it would give me a vector2 or the z value would be a 0.

local screenPos = UDim2.new(0, touch.Position.X, 0, touch.Position.Y)

Edit: Just checked, it is zero I’m just dumb.