I’m trying to make a 2D game and right now I’m making the movement
I want to make something like the title says
robloxapp-20220918-1315534.wmv (1.0 MB)
The character is walking at left and I want him to look right while he is doing that.
Here is my script :
local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local char = player.Character or player.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")
local jumping = false
local leftValue, rightValue = 0,0
local lastDirection, currentDirection = 0, 0
local AnimationTrack
local function playerOrient(angle)
if lastDirection~=currentDirection then
local HRP = player.Character:WaitForChild("HumanoidRootPart")
local Rootjoint = player.Character.Torso:WaitForChild("Neck")
local x, y, z = HRP.CFrame:ToEulerAnglesYXZ()
HRP.CFrame = CFrame.new(HRP.Position) * CFrame.Angles(0, angle, 0)
end
end
local function onLeft(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
local animation = Instance.new("Animation")
local animationId = 10927684449
animation.AnimationId = "rbxassetid://" ..animationId
AnimationTrack = hum:LoadAnimation(animation)
AnimationTrack:Play(0.100000001, 1, -1)
leftValue = 1
currentDirection = -1
playerOrient(math.rad(90))
lastDirection = -1
elseif inputState == Enum.UserInputState.End then
AnimationTrack:Stop()
leftValue = 0
end
end
local function onRight(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
rightValue = 1
currentDirection = 1
playerOrient(math.rad(270))
lastDirection = 1
elseif inputState == Enum.UserInputState.End then
rightValue = 0
end
end
local function onJump(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
jumping = true
elseif inputState == Enum.UserInputState.End then
jumping = false
end
end
local function onUpdate()
if player.Character and player.Character:FindFirstChild("Humanoid") then
if jumping then
player.Character.Humanoid.Jump = true
end
local moveDirection = rightValue - leftValue
player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
end
end
RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)
ContextActionService:BindAction("Left", onLeft, true, Enum.KeyCode.A, Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction("Right", onRight, true, Enum.KeyCode.D, Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
ContextActionService:BindAction("Jump", onJump, true, Enum.KeyCode.W, Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp)