Hey there, I am creating a 2D platformer game with a notion a 3d that explains itself by pressing on “W” to go backward and “S” to go forward, a screen is provided [.gif 1].
.gif 1: https://gyazo.com/636dab65645ce19a18b4dbd85c635a7d
The plans are divided in 3 areas [screen 2]
screen 2: https://gyazo.com/d512ab6642f3db5fd6fc966e51cd1571
So I disabled Z Axis controls in the Control Script that is apart of ROBLOX Core.
The thing is, I tried so much things to move the Character. I tried “Humanoid:Move()
”, “Humanoid:MoveTo()
”, “TweenService
”, … I don’t know what to use in order to move the Character on the Z Axis…
So, I write this message to have some advices if possible. This is the final scripts I had written so far (note that they are tests, and the code may be dirty):
Just before showing it: the “plan” is the used term to determine if the player is at the foreground (plan = 1), middle-ground (plan = 2) or background (plan = 3).
--In a localScript located in StarterGui
--[Varonex]--
local UIS = game:GetService("UserInputService")
local cScript = require(game:GetService("Players").LocalPlayer.PlayerScripts.PlayerModule):GetControls() --Getting the Control Script of the ROBLOX Core
local debounce2 = false
local plan = 2 --The player is by default on the "Middle-ground"
local coordinates = {-7.5, -11.35, -15} --Coordinates to move player to on the Z Axis, respectively "Foreground", "Middle-ground", "Background"
local Player = game.Players.LocalPlayer
repeat wait()
until Player.Character:FindFirstChild("HumanoidRootPart")
local Char = Player.Character
local HumP = Char.HumanoidRootPart
local ignoreList = {Char, workspace.Area} --List of ignored items when launching a raycast (the part under this line.) The elements located in "workspace.Area" are separating walls in order to stabilize the player in each "plan".
--Function that determines if there is a part behind the character when attempting to go backward. A raycast is launched.
function onDetermineSpaceUp()
local origin = HumP.Position
local lookDirection = Vector3.new(0, 0, -2.5)
local ray = Ray.new(origin, lookDirection)
local hitPart, hitPosition = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
return hitPart, hitPosition
end
--Function that determines if there is a part in front of the character when attempting to go forward. A raycast is launched.
function onDetermineSpaceDown()
local origin = HumP.Position
local lookDirection = Vector3.new(0, 0, 2.5)
local ray = Ray.new(origin, lookDirection)
local hitPart, hitPosition = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
return hitPart, hitPosition
end
UIS.InputBegan:Connect(function(input)
if not debounce2 then
if input.KeyCode == Enum.KeyCode.W then
if plan == 3 then
plan = 3
elseif plan < 3 then
local hitPart, hitPosition = onDetermineSpaceUp()
if not hitPart then
debounce2 = true --Disabling the ability to change plan (refer to the beginning of this function.)
local Humanoid = Char:WaitForChild("Humanoid")
plan = plan + 1
cScript:Disable() --Disable controls
Humanoid.WalkSpeed = 45
--The parts located in "workspace.Area" are here to stabilize the Character and are marking the separation of each "plan".
for i, v in pairs(workspace.Area:GetChildren()) do
v.CanCollide = false --"Disabling" these walls
end
Humanoid:MoveTo(Vector3.new(HumP.Position.X, HumP.Position.Y, coordinates[plan]), nil)
wait(.15)
Humanoid.WalkSpeed = 16
for i, v in pairs(workspace.Area:GetChildren()) do
v.CanCollide = true --"Enabling" these walls
end
debounce2 = false --The Character can change plan now (refer to the beginning of this function.)
cScript:Enable() --Re-enable controls. Note that the user must push again the key to go left or right.
end
end
elseif input.KeyCode == Enum.KeyCode.S then
if plan == 1 then
plan = 1
elseif plan > 1 then
local hitPart, hitPosition = onDetermineSpaceDown()
if not hitPart then
debounce2 = true --Disabling the ability to change plan (refer to the beginning of this function.)
local Humanoid = Char:WaitForChild("Humanoid")
plan = plan - 1
cScript:Disable() --Disable controls.
Humanoid.WalkSpeed = 45
--The parts located in "workspace.Area" are here to stabilize the Character and are marking the separation of each "plan".
for i, v in pairs(workspace.Area:GetChildren()) do
v.CanCollide = false --"Disabling" these walls
end
Humanoid:MoveTo(Vector3.new(HumP.Position.X, HumP.Position.Y, coordinates[plan]), nil)
wait(.15)
Humanoid.WalkSpeed = 16
for i, v in pairs(workspace.Area:GetChildren()) do
v.CanCollide = true --"Enabling" these walls
end
debounce2 = false --The Character can change plan now (refer to the beginning of this function.)
cScript:Enable() --Re-enable controls. Note that the user must push again the key to go left or right.
end
end
end
end
print(plan) --This is a simple check ._.
end)
--[Varonex]--
They must not be optimized. They were just beta scripts to see what system should I use.
This script was an example of what I did with Humanoid:MoveTo()
. Now the thing I want to do is a more stable, and fluid method to use.
Final question: Should I code a new specific script to map movements instead of ROBLOX one ?
Thank you all for your asnwers. I am sorry if I did something wrong by opening this topic, it is my first topic.