Controlling a part by a mouse while keeping it in another part

Sorry for the title, doesn’t explain the situation well.

I wanted to make a user-controllable part controlled by a mouse. The catch is that the part would have to stay in another (area) part for the whole time. Plus of course it can only slide on the ground and on the edges of the area part. I have already tried a few methods but hasn’t come up with a satisfying solution yet. The part ‘wiggles’ and continues to glitch sometimes.

Here’s the video material:


Notes:

  • the area part can be seen at then end, when I close out of the test session
  • the yellow part is a part which provides some light (anchored, can’t collide).

I currently use network ownership to achieve everything shown in the video, however I plan to switch to remote events (more control over the part, the part needs to be anchored and with network ownership - it can not).

Here is a part of the code which calculates where to teleport the part (Local Script):

local workspaceW = game:GetService("Workspace")
local baseplate = workspaceW:WaitForChild("Baseplate")

local function rayResult(x, y)
	local unitRay = camera:ViewportPointToRay(x, y)
	local origin = unitRay.Origin
	local direction = unitRay.Direction

	return workspaceW:Raycast(origin, direction * 250, params), origin, direction
end

local mouseLocation = game:GetService("UserInputService"):GetMouseLocation()
local result, origin, direction = rayResult(mouseLocation.X, mouseLocation.Y)
local placeToTpTo = CFrame.new(result.Position)
local baseplateCenter = baseplate.CFrame.LookVector * (baseplate.Size.Z / 2)
local partHeight = part.Size.Y
local placeToTpTo = CFrame.new(placeToTpTo.Position.X, baseplateCenter.Y + (partHeight/2), placeToTpTo.Position.Z)
part:PivotTo(placeToTpTo)

Notes:

  • the script makes a few checks before teleporting, for example if the part is in the area box (it doesn’t work as expected tho because the part can sometimes glitch out)
  • in the actual script placeToTpTo value is determined on whenever the part’s new position will be in or out of the box (if out then it uses the previous in position and teleports to it)
  • as this is just a prototype and I didn’t want the part to also glitch out in the baseplate’s floor, I add baseplateCenter.Y to the part’s height divided by 2.

I currently have no idea on how to calculate a perfect position and make the system comfortable and satisfying to use.

Game reference: hockey world v-slice - Roblox, say pg/me on the chat

Thanks for all of the help and if there’s anything that isn’t clear, just ask me in the comments :slightly_smiling_face:

I can’t see the glitching or the wobbles, it looks a nice and clean movement to me until it moves beyond the allocated space. I assume you mean that if the mouse moves quick enough the block doesn’t then move outside the boundaries you have set?

  • By glitching and wobbling I mean what the block does when it touches the area part’s walls.
  • Plus you can also glitch out of the area part as seen on the video (0:08-0:14).
  • I am not sure what you mean here, but the block shouldn’t move out of those boundaries. Also when moving your mouse fast enough you are able to do the glitches I’ve mentioned in this message above.
1 Like

Okay, I understand now. It was hard to see the problem, I could see it glitching beyond the boundaries. I’m not sure what correction system you want when the mouse moves out of bounds, you could either fix the position on the last correct mouse position that happened and leave it there, or allow it to move with the mouse smoothly along the boundaries but only using the axis that is still within bounds. I’ll have a chew on this, test some code, and hopefully help you with something.