플레이어의 체력을 감소시키는 함정은 조건문으로 코딩할 수 있는 재미있는 게임 요소입니다. 접촉할 때 플레이어의 체력을 0으로 설정하는 부품을 만들어 조건문을 연습해 보세요.
함정 설정하기
함정은 오비와 같은 움직임 기반 도전 과제를 가진 경험에서 매우 효과적입니다. 이 단계에서는 필요한 변수와 함수를 설정하는 것으로 시작합니다. 코드 상자를 보기 전에 할 수 있는 만큼 해보세요.
함정 부품을 만들고 이름을 지정합니다. 부품에 스크립트를 삽입합니다.
스크립트에 설명하는 주석을 추가한 다음 변수를 사용하여 스크립트의 부모를 참조합니다.
-- 플레이어가 이 부품에 접촉하면 체력을 0으로 설정합니다.local trapPart = script.ParentotherPart라는 매개변수를 가진 onTouch()라는 이름의 함수를 생성합니다.
-- 플레이어가 이 부품에 접촉하면 체력을 0으로 설정합니다.local trapPart = script.Parentlocal function onTouch(otherPart)end함수를 함정 부품의 Touched 이벤트에 연결하여 무언가가 부품에 접촉할 때마다 실행되도록 합니다.
local trapPart = script.Parentlocal function onTouch(otherPart)endtrapPart.Touched:Connect(onTouch)
플레이어 접촉 확인하기
기억하세요, 매개변수 otherPart는 함정 부품에 접촉하는 모든 것을 기록합니다. 이는 플레이어의 일부일 수도 있고 단순히 바닥일 수도 있습니다.
함정이 플레이어만 파괴하도록 하고 무작위 장식 아이템을 파괴하지 않도록 하기 위해, otherPart에 Humanoid 객체가 포함되어 있는지 확인하기 위해 if/then 구문을 사용합니다.
특정 객체 찾기
FindFirstChildWhichIsA() 함수를 사용하여 특정 객체 유형을 찾을 수 있습니다. 이는 Humanoid 유형의 객체를 찾고 있기 때문에 유용합니다. 플레이어는 일반적으로 아바타의 일부로 함정에 접촉하기 때문에 접촉하는 부품의 부모를 찾고 Humanoid를 검색하기 위해 변수를 설정해야 합니다.
onTouch()에서 local character = otherPart.Parent를 입력합니다.
local trapPart = script.Parentlocal function onTouch(otherPart)-- otherPart의 부모 객체를 찾습니다.local character = otherPart.ParentendtrapPart.Touched:Connect(onTouch)character가 Humanoid를 가지고 있는지 확인합니다. 다음을 입력합니다:
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")endtrapPart.Touched:Connect(onTouch)
if 문으로 확인하기
Humanoid가 발견되면 Humanoid의 Health를 0으로 설정합니다.
Humanoid가 local humanoid에 성공적으로 할당되었는지 확인하기 위해 if 문을 사용합니다.
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Humanoid가 발견되었는지 평가합니다.if humanoid thenendendtrapPart.Touched:Connect(onTouch)print 문을 추가하고 지금까지의 코드를 확인합니다.
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Humanoid가 발견되었는지 평가합니다.if humanoid thenprint("Humanoid를 찾았습니다")endendtrapPart.Touched:Connect(onTouch)코드를 실행하고 플레이어가 부품에 접촉할 때 출력이 보이는지 확인합니다.
플레이어의 체력 변경하기
진술이 참인 경우 동일한 humanoid 변수를 사용하여 플레이어의 체력을 0으로 설정할 수 있습니다.
then과 end 사이에 humanoid.Health = 0을 입력합니다.
완료된 스크립트local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Humanoid가 발견되었는지 평가합니다.if humanoid thenprint("Humanoid를 찾았습니다")humanoid.Health = 0endendtrapPart.Touched:Connect(onTouch)함정을 테스트합니다.
요약
이 함정 부품은 조건문을 사용하여 Humanoid 부품을 감지하고 Humanoid의 체력을 0으로 설정했습니다. 이 스크립트는 접촉하는 객체가 무엇이든 상관없이 파괴하던 이전 함정 스크립트의 개선 사항입니다.
그러나 여전히 몇 가지 결점이 있습니다. Humanoid는 플레이어만 있는 것이 아닙니다. Humanoid는 NPC에서도 발견됩니다. 이 스크립트는 플레이어의 체력을 0으로 설정하는 데만 유용합니다. 작은 체력을 빼는 것을 실험할 수 있지만, 체력을 원하는 것보다 더 빨리 빼는 경우가 많습니다. 이후 수업은 플레이어의 체력이 얼마나 줄어드는지를 보다 잘 제어할 수 있도록 추가 개선 사항을 제공합니다.