Center Stud Moving

  1. What do you want to achieve?
    I would like my character to move in the centre of the stud and not along a line

  2. What is the issue?

  3. What solutions have you tried so far?
    Roblox forum, youtube

I think you can move your baseplace

My script probably has the wrong calculation


local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid")
local moveStep = 4
local isMoving = false

humanoid.WalkSpeed = 0

local function moveCharacter(direction)
	if humanoid and not isMoving then
		isMoving = true

		local newPosition = character.PrimaryPart.Position + direction * moveStep

		local roundedPosition = Vector3.new(
			math.floor(newPosition.X / moveStep + 0.5) * moveStep
			newPosition.Y, 
			math.floor(newPosition.Z / moveStep + 0.5) * moveStep
		)

		local currentCFrame = character.PrimaryPart.CFrame
		local rotationOnly = CFrame.new(Vector3.new(), currentCFrame.lookVector)

		character:SetPrimaryPartCFrame(CFrame.new(roundedPosition) * rotationOnly)

		print("New Position: ", newPosition)
		print("Rounded Position: ", roundedPosition)

		wait(0.1)
		isMoving = false
	end
end

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end

	if not isMoving then
		local moveDirection = Vector3.new(0, 0, 0)

		if input.KeyCode == Enum.KeyCode.W then
			moveDirection = Vector3.new(1, 0, 0) 
		elseif input.KeyCode == Enum.KeyCode.S then
			moveDirection = Vector3.new(-1, 0, 0)
		elseif input.KeyCode == Enum.KeyCode.A then
			moveDirection = Vector3.new(0, 0, -1)
		elseif input.KeyCode == Enum.KeyCode.D then
			moveDirection = Vector3.new(0, 0, 1)
		end

		print("Move Direction: ", moveDirection)

		local targetPosition = character.PrimaryPart.Position + moveDirection * moveStep
		local ray = Ray.new(character.PrimaryPart.Position, (targetPosition - character.PrimaryPart.Position).unit * moveStep)
		local hit, position = workspace:FindPartOnRay(ray, character)

		print("Hit: ", hit)
		print("Position: ", position)

		if not hit or (hit.Position - character.PrimaryPart.Position).magnitude < moveStep then
			moveCharacter(moveDirection)
		end
	end
end)


I think the most simple solution to this problem is to move the spawnLocation to the center of the stud.

I tried to do this then automatically my character teleports to the line

Id say whatever the problem is, it has something to do with the roundedPosition. For testing purposes, try putting everything into their own print() functions and checking the math yourself

Have you tried offsetting the texture?

math.floor(newPosition.X / moveStep + 0.5)             * moveStep

this is a round-up, but it lands ‘on the line’ because the floor returns integer

so we should also add 0.5 to move it ‘on the stud’, and then multiply by moveStep

(math.floor(newPosition.X / moveStep + 0.5) + 0.5)     * moveStep
1 Like

the character behaves strangely now after changing to this and walks in the wrong direction

I fixed it with code:

thanks for your help

local roundedPosition = Vector3.new(
(math.floor((newPosition.X - 0.5 * moveStep) / moveStep + 0.5)) * moveStep + 0.5 * moveStep,
newPosition.Y,
(math.floor((newPosition.Z - 0.5 * moveStep) / moveStep + 0.5)) * moveStep + 0.5 * moveStep
)

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