Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

2D custom player movement Jitters?

Asked by 4 years ago
Edited 4 years ago

I'm attempting to make a little 2D top down game.

Currently, I'm working on the custom movement, I've got the camera following the player by being welded to it, but the player seems to "jitter", and if I tap between left and right movement quickly I go double speed, any idea on how I might be able to fix these?

I've tried to Tween movement and it just either slowed the player down or didn't have any effect on the problem.

Here is a video of the problem

https://i.imgur.com/PgW65jY.mp4

Here are snippets of the code:

in one of the two localscripts

local InputService = game:GetService("UserInputService")

InputService.InputBegan:Connect(function(key)
    if game.Players.LocalPlayer.Character ~= nil then --If I want to have a custom menu that has spawning the player as the trigger for movement
        if key.KeyCode == Enum.KeyCode.W then -- If player pressed W key
            if InputService:IsKeyDown(Enum.KeyCode.S) ~= true then -- and if player is not pressing the S key
                script.Y.Value = script.Speed.Value -- Set Y (An intvalue that works with the script below) to a set speed
            else -- If player is pressing W and S then stop Y movement
                script.Y.Value = 0
            end
        end
        --Pretty much duplicates of the same code for A S and D

in the other local script

local Player = game.Workspace.Players:WaitForChild(game.Players.LocalPlayer.Name) -- PlayerModel that holds the Player Decal object
script.Parent.Changed:Connect(function() -- If the Parent(a intvalue named Y) changed
    while script.Parent.Value ~= 0 do -- While the value is something other then 0
        wait()
        Player.Player.CFrame = CFrame.new(Player.Player.Position + Vector3.new(0, 0, script.Parent.Value/10)) -- Add the speed to the playerobject position
        if script.Parent.Value == 0 then -- Break the loop if the player stops moving
            break
        end
    end
end)
--This is moving in "Y" (ZAxisMovement)
--There is also another version of the same script that applies to the X movement

I tried my best at commenting the scripts, please ask any questions.

0
edit:- fixed link User#5423 17 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

While watching some Youtube I thought of a solution

The way I have the camera set up is having a while wait() do set the camera to the player, which would be fine, but it seems that it doesn't go at the same time the player moves, understandable.

To fix this I made the initial code that makes the camera face the player happen only once to set the camera in the first place, then every time the player is moved, the camera moves along side it by just adding the 2 lines of code that moves the camera initially with the code that moves the player.

Player.Player.CFrame = CFrame.new(Player.Player.Position + Vector3.new(script.Parent.Value/10, 0, 0)) -- PlayerMoves
local cFrame = CFrame.new(CameraObject.Position, PlayerObject.Position) --Start Camera Movement
Camera.CFrame = cFrame * CFrame.fromEulerAnglesXYZ(math.rad(0), math.rad(0), math.rad(-90))
Ad

Answer this question