Detect if Mouse is on one of these sides of a cylinder

Hello All!

I’m wondering if it’s possible to know if a player’s mouse is on of these sides (blue parts are just there to show what sides i’m talking about).



(2 Images just to show position and size XYZ axis, cylinder’s size is 7, 1.4, 1.4 incase it’s needed).

This probably needs alot of math done, but i’m willing to learn. Thank you! :smile:

1 Like

Just a check , are you detecting normal(the surface) or are you trying to detect the edges?

1 Like

I’ve tried detecting edges yesterday and it worked (not very consistent with mouse placing though) except for the part on top of the cylinder, so I scrapped the code. But if theres multiple approach, i’d like to see and know how they work.

EDIT: Nevermind, I still have the code. But basically this is what I did.

if horizontalMode == "Stack" then
	local nearestLog = hitPart
	local logHeight = nearestLog.Size.Y

	local stackOffset = Vector3.new(0, logHeight, 0)
	local stackPosition = nearestLog.Position + stackOffset

	logGuide.CFrame = CFrame.new(stackPosition)
	logGuide.Orientation = nearestLog.Orientation
elseif horizontalMode == "Extend" then
	local nearestLog = hitPart
	local logSize = nearestLog.Size
	local logPosition = nearestLog.Position
	local logOrientation = nearestLog.Orientation

	local topEndpoint, bottomEndpoint
	local halfLength = logSize.X / 2
	topEndpoint = logPosition + nearestLog.CFrame:vectorToWorldSpace(Vector3.new(halfLength, 0, 0))
	bottomEndpoint = logPosition - nearestLog.CFrame:vectorToWorldSpace(Vector3.new(halfLength, 0, 0))

	local mousePosition = mouse.Hit.Position
	local distanceToTop = (mousePosition - topEndpoint).Magnitude
	local distanceToBottom = (mousePosition - bottomEndpoint).Magnitude

	if distanceToTop < distanceToBottom then
		local directionToPlace = (mousePosition - topEndpoint).Unit

		if math.abs(directionToPlace.X) > math.abs(directionToPlace.Z) then
			if directionToPlace.X < 0 then
				--print("top")
				if nearestLog.Orientation.Y == 0 then
					logGuide.CFrame = CFrame.new(topEndpoint - Vector3.new(-logSize.X/2,0,0))
					logGuide.Orientation = Vector3.new(0, 0, 0)
				else
					logGuide.CFrame = CFrame.new(topEndpoint - Vector3.new(0,0,logSize.Z*2.5))
					logGuide.Orientation = Vector3.new(0, 90, 0)
				end
			end
		else
			if directionToPlace.Z > 0 then
				--print("left top")
				if nearestLog.Orientation.Y == 0 then
					logGuide.CFrame = CFrame.new(topEndpoint + Vector3.new(0, 0, logSize.Z*2.5))
					logGuide.Orientation = Vector3.new(0, 90, 0)
				else
					logGuide.CFrame = CFrame.new(topEndpoint + Vector3.new(logSize.X/2, 0, 0))
					logGuide.Orientation = Vector3.new(0, 0, 0)
				end
			else
				--print("right top")
				if nearestLog.Orientation.Y == 0 then
					logGuide.CFrame = CFrame.new(topEndpoint - Vector3.new(0, 0, logSize.Z*2.5))
					logGuide.Orientation = Vector3.new(0, 90, 0)
				else
					logGuide.CFrame = CFrame.new(topEndpoint - Vector3.new(logSize.X/2, 0, 0))
					logGuide.Orientation = Vector3.new(0, 0, 0)
				end

			end
		end
	else
		local directionToPlace = (mousePosition - bottomEndpoint).Unit

		if math.abs(directionToPlace.X) > math.abs(directionToPlace.Z) then
		        --print("bottom")
			if nearestLog.Orientation.Y == 0 then
				logGuide.CFrame = CFrame.new(bottomEndpoint + Vector3.new(-logSize.X/2,0,0))
				logGuide.Orientation = Vector3.new(0, 0, 0)
			else
				logGuide.CFrame = CFrame.new(bottomEndpoint + Vector3.new(0,0,logSize.Z*2.5))
				logGuide.Orientation = Vector3.new(0, 90, 0)
			end

		else
			if directionToPlace.Z > 0 then
				--print("left bot")
				if nearestLog.Orientation.Y == 0 then
					logGuide.CFrame = CFrame.new(bottomEndpoint + Vector3.new(0, 0, logSize.Z*2.5))
					logGuide.Orientation = Vector3.new(0, 90, 0)
				else
					logGuide.CFrame = CFrame.new(bottomEndpoint + Vector3.new(logSize.X/2, 0, 0))
					logGuide.Orientation = Vector3.new(0, 0, 0)
				end
			else
				--print("right bot")
				if nearestLog.Orientation.Y == 0 then
					logGuide.CFrame = CFrame.new(bottomEndpoint - Vector3.new(0, 0, logSize.Z*2.5))
					logGuide.Orientation = Vector3.new(0, 90, 0)
				else
					logGuide.CFrame = CFrame.new(bottomEndpoint - Vector3.new(logSize.X/2, 0, 0))
				    logGuide.Orientation = Vector3.new(0, 0, 0)
				end
			end
		end
	end
end

I got it!

I basically created EndPoints for Top,Mid and Bottom using VectorToWorldSpace().


image
image
image

local halfLength = logSize.X / 2
local topEndpoint = logPosition + nearestLog.CFrame:vectorToWorldSpace(Vector3.new(halfLength, 0, 0))
local bottomEndpoint = logPosition - nearestLog.CFrame:vectorToWorldSpace(Vector3.new(halfLength, 0, 0))
local middlePoint = logPosition

local mousePosition = mouse.Hit.Position
local distanceToTop = (mousePosition - topEndpoint).Magnitude
local distanceToBottom = (mousePosition - bottomEndpoint).Magnitude
local distanceToMiddle = (mousePosition - middlePoint).Magnitude

--checks if mouse is closer to middle than top or bottom endpoints
if distanceToMiddle < 2 then
	local stackOffset = Vector3.new(0, logSize.Y, 0)
	local stackPosition = middlePoint + stackOffset
	logGuide.CFrame = CFrame.new(stackPosition)
	logGuide.Orientation = nearestLog.Orientation
else
	--compares which one is closer then chooses topEndpoint if top is closer otherwise bottom.
	local selectedEndpoint = (distanceToTop < distanceToBottom) and topEndpoint or bottomEndpoint

	local directionToPlace = (mousePosition - selectedEndpoint).Unit
	local placePosition = selectedEndpoint
	local newOrientation = Vector3.new(0, 0, 0)
	
	if math.abs(directionToPlace.X) > math.abs(directionToPlace.Z) then
		-- Horizontal placement logic (X-axis)
		
	else
		-- Vertical placement logic (Z-axis)
		
	end

	logGuide.CFrame = CFrame.new(placePosition)
	logGuide.Orientation = newOrientation
end

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