게임

로블록스 스튜디오 루아 스크립트로 달리기 효과 구현

패스자 2025. 4. 17. 21:32

Shift 키를 눌렀을 때 달리고, Shift 키를 떼면 걷기.
------------------------------------------------------------
-- 달리기
------------------------------------------------------------
local UserInput = game:GetService('UserInputService')
local LocalPlayer = game:GetService("Players").LocalPlayer


------------------------------------------------------------
-- 변수
------------------------------------------------------------
local Humanoid = script.Parent:WaitForChild('Humanoid')

local WalkSpeed = 16
local RunSpeed = 30


------------------------------------------------------------
-- 플레이어 속도 조절
------------------------------------------------------------
local function ChangeSpeed(speed)
	Humanoid.WalkSpeed = speed
end


------------------------------------------------------------
-- 버튼 입력이 들어올 때.
------------------------------------------------------------
UserInput.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.LeftShift then -- 입력한 키가 Shift 라면,
				ChangeSpeed(RunSpeed)
			end
		end
	end
end)


------------------------------------------------------------
-- 버튼 입력이 끝날 때.
------------------------------------------------------------
UserInput.InputEnded:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.LeftShift then -- 입력한 키가 Shift 라면,
				ChangeSpeed(WalkSpeed)
			end
		end
	end
end)

 

Shift 키를 한번 눌렀을 때 달리고, 다시 Shift 키를 누르면 걷기 상태로 돌아옴.
------------------------------------------------------------
-- 달리기
------------------------------------------------------------
local UserInput = game:GetService('UserInputService')
local LocalPlayer = game:GetService("Players").LocalPlayer


------------------------------------------------------------
-- 변수
------------------------------------------------------------
local Humanoid = script.Parent:WaitForChild('Humanoid')

local WalkSpeed = 16
local RunSpeed = 30

local IsWalking = true


------------------------------------------------------------
-- 플레이어 속도 조절
------------------------------------------------------------
local function ChangeSpeed(speed)
	Humanoid.WalkSpeed = speed
end


------------------------------------------------------------
-- 버튼 입력이 들어올 때.
------------------------------------------------------------
UserInput.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.LeftShift then -- 입력한 키가 Shift 라면,
				if IsWalking then -- 걷고 있다면,
					ChangeSpeed(RunSpeed) -- 달리는 속도로 바꿈.
					IsWalking = false
				else -- 달리고 있다면,
					ChangeSpeed(WalkSpeed) -- 걷는 속도로 바꿈.
					IsWalking = true
				end
				-- ChangeSpeed(IsWalking and RunSpeed or WalkSpeed) -- Lua 삼항연산자.
				-- IsWalking = not IsWalking
			end
		end
	end
end)

 

속도가 달라질 때, 달리기 모션도 따로 넣어야하는 줄 알았는데, Humanoid.WalkSpeed가 높아질수록 기본 걷는 애니메이션이 자동으로 배속되면서 빨라진다. 이 스크립트는 StarterPlayer -> StarterCharacterScripts 폴더 밑에 넣어주면 작동한다.

StarterCharacterScripts

여기 폴더 밑에 있는 스크립트는 캐릭터가 생성될 때 호출된다. (죽었다 살아나도 재생)

 

StarterPlayerScripts

여기 폴더 밑에 있는 스크립트는 플레이어가 들어올 때 호출된다. (위의 코드로는 죽었다 살아나면 달리기가 안된다. 코드 수정 필요)

 

로블록스 루아 스크립트 (Lua Script) 기초