|
| 09 Feb 2015 05:37 PM |
I tried making the movement less choppy by putting a while loop. How would I make it if let's say I wen't right, it wouldn't let me go left until I stopped going right?
Code:
local Player = game.Players.LocalPlayer local GuiPlayer = script.Parent local Mouse = Player:GetMouse()
function moveRight(Key) if Key == "d" then print("Moving right") while wait() do GuiPlayer.Position = GuiPlayer.Position +UDim2.new(0.01, 0, 0, 0) end end end
function moveLeft(Key) if Key == "a" then print("Moving left") while wait() do GuiPlayer.Position = GuiPlayer.Position -UDim2.new(0.01, 0, 0, 0) end end end
function moveUp(Key) if Key == "w" then print("Moving up") end end
function moveDown(Key) if Key == "s" then print("Moving down") end end
function Jump(Key) if Key:byte() == 32 then print("Jumping") end end
Mouse.KeyDown:connect(moveRight) Mouse.KeyDown:connect(moveLeft) Mouse.KeyDown:connect(moveUp) Mouse.KeyDown:connect(moveDown) Mouse.KeyUp:connect(Jump)
"I like to program." - Bosswalrus |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2015 05:44 PM |
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse()
local Keys = { w = false; a = false; s = false; d = false; e = false; q = false; }
local Invert = function(key) Keys[key] = not Keys[key] end
Mouse.KeyDown:connect(Invert) Mouse.KeyUp:connect(Invert)
while true do Player.Character.Torso.CFrame = Character.Torso.CFrame * CFrame.new( a and -1 or d and 1, e and 1 or q and -1, a and -1 or d and 1 ) wait(0) end -- TODO: Use renderstepped |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 09 Feb 2015 05:52 PM |
local player = game.Players.LocalPlayer; local guiPlayer = script.Parent;
local mouse = player:GetMouse(); local MOVE_SPEED = 0.01;
local keys = { UP = false; LEFT = false; RIGHT = false; DOWN = false; };
local updateKey = function (key, pressed) if (key == "w") then keys.UP = pressed; elseif (key == "a") then keys.LEFT = pressed; elseif (key == "d") then keys.RIGHT = pressed; elseif (key == "s") then keys.DOWN = pressed; end end;
local onKeyDown = function (key) updateKey(key, true); end;
local onKeyUp = function (key) updateKey(key, false); end;
local movePlayer = function (player, moveVec) player.Position = player.Position + moveVec; end;
local handleInput = function (deltaTime) local dx = 0; local dy = 0;
if (keys.UP) then dy = dy - 1; end if (keys.DOWN) then dy = dy + 1; end
if (keys.LEFT) then dx = dx - 1; end if (keys.RIGHT) then dx = dx + 1; end
local dd = dx*dx + dy*dy; if (dd > 0) then -- Not sure on this bit dd = math.sqrt(dd);
dx = dx / dd; dy = dy / dd; end
local moveX = dx * deltaTime * MOVE_SPEED; local moveY = dy * deltaTime * MOVE_SPEED;
local moveVec = UDim2.new(moveX, 0, moveY, 0);
movePlayer(guiPlayer, moveVec); end;
local update = function (deltaTime) handleInput(deltaTime); end;
local main = function () while (true) do local deltaTime = wait(); -- I think this is correct update(deltaTime); end; end;
mouse.KeyDown:connect(onKeyDown); main(); |
|
|
| Report Abuse |
|
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
| |
|
|
| 09 Feb 2015 06:02 PM |
| What a big script for no reason ._. |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2015 08:48 PM |
Wouldn't tweening it make it go choppy. Also that code is giant, I'll try and research it tomorrow.
"I like to program." - Bosswalrus |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2015 04:13 PM |
i prefer to do deltaTime like this:
local dt, lastStep;
game:GetService("RunService").RenderStepped:connect(function() dt = lastStep and tick() - lastStep or 1 / 30 --1 / 30 is a good estimation for when lastStep doesn't yet exist lastStep = tick()
end) |
|
|
| Report Abuse |
|
|
drager980
|
  |
| Joined: 25 May 2009 |
| Total Posts: 13385 |
|
|
| 10 Feb 2015 04:14 PM |
@FD, arent you like a day late |
|
|
| Report Abuse |
|
|
| |
|