โฐTick
๐ฎGame Loop
- ๊ฒ์ ์์ง์ด ํ๋ ์ด์ด์ ์ธํ์ ๊ฐ์ ธ์ด
- ์ธํ ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ ๊ฒ์ ์ํ ์
๋ฐ์ดํธ
- ๊ฒ์ ์์ง์์ ๊ฒ์ ์ํ๋ฅผ ๊ฐ์ ธ์ ํ๋ ์ด์ด, ์นด๋ฉ๋ผ ๋ฑ์ ์์น๋ฅผ ์ ํ ๋ค์ ํ๋ฉด์ ๋ ๋๋ง / ์ผ๋ จ์ ์ซ์๋ค์ด ํ๋ฉด์ ํ์๋ ์ด๋ฏธ์ง๋ก ๋ฐ๋
๐์ฉ์ด ์ ๋ฆฌ
- ํ๋ ์
: ๋์์์ ๊ตฌ์ฑํ๋ ๊ทธ๋ฆผ ์ค ํ๋
- ํ๋ ์๋ฅ (FPS)
: ์ปดํจํฐ๊ฐ 1์ด๋น ํต๊ณผํ ์ ์๋ ๊ฒ์ ๋ฃจํ์ ์๊ณ
- ์ด๋น ํ๋ ์ ์
: ์ธํ์ ์ฒ๋ฆฌํ๊ณ ์ํ๋ฅผ ์
๋ฐ์ดํธํ์ฌ ๋ ๋๋งํ๋ ๋น๋
โ ์ธ๋ฆฌ์ผ์์๋ Tick ํจ์๋ฅผ ์ฌ์ฉํด ๊ฒ์ ๋ฃจํ์ ์ค๊ฐ ๋จ๊ณ๋ฅผ ์งํํจ
- Tick
: ๋ชจ๋ ํ๋ ์ ํธ์ถ, ์ด๋ฒคํธ๋ก ๋ธ๋ฃจํ๋ฆฐํธ์๋ ์กด์ฌํ๋ ํจ์
๐จC++ ์์
๐จ
- ํ๋ซํผ ์ด๋์ํค๋ ์์
,
ํ๋ซํผ์ ํฌ๊ธฐ๋ฅผ ๋ฉ์ํ ์ง์ก๋ฉด์ฒด ๋ชจ์์ผ๋ก ๋ฐ๊พธ๊ณ ํจ์์ ๊ธฐ๋ฅ ์ถ๊ฐ
void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
MyVector.Y = MyVector.Y + 1;
SetActorLocation(MyVector);
}
๐ฒ์ง์ญ ๋ณ์(Local Variables)
๐ Scope
- ๋ณ์๊ฐ ์ ์ง๋๋ ๊ธฐ๊ฐ ์๋ฏธ
- ์ผ๋ฐ์ ์ผ๋ก C++์์ ๋ณ์๋ ๊ตฌ๋ฌธ์ ์ค๊ดํธ ์ฌ์ด์ ์์ ๋ ์ ํจํจ
void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector LocalVector = MyVector; //์ง์ญ ๋ณ์!!
LocalVector.Z = LocalVector.Z + 100;
MyVector.Y = MyVector.Y + 1;
SetActorLocation(LocalVector);
}
๐ฌ์์ฌ(Pseudo) ์ฝ๋
๐์ฉ์ด ์ ๋ฆฌ
- ์์ฌ์ฝ๋
: ์๊ณ ๋ฆฌ์ฆ์์ ์ฌ์ฉํ ๋จ๊ณ๋ฅผ ์ผ๋ฐ์ ์ธ ์ธ์ด๋ก ์ค๋ช
ํ ๊ฒ
- ์ฃผ์
: ์ปดํ์ผ๋ฌ์์ ๋ฌด์ํ๋ ์ฝ๋
๐จC++ ์์
๐จ
- ๋ค์์ผ๋ก ํ ์์
์ฃผ์์ผ๋ก ์์ฑํด๋๊ธฐ
// Move platform forwards
// Get current location
// Add vector to that location
// Set the location
// Send platform back if gone too far
// Check how far we've moved
// Reverse direction of motion if gone too far
๐คํจ์ ๋ฐํ ๊ฐ
๐์ฉ์ด ์ ๋ฆฌ
- ํํ์
: ๊ฐ์ ์์ฑํ๋ ์ฝ๋ ์กฐ๊ฐ
- ๋ฌธ (Statement=์ฝ๋ ์กฐ๊ฐ)
: ์ํ๋์ด์ผ ํ ๋์
๐ํํ์(Expressions)
- MyVector
- LocalVector.Z + 100
FVector LocalVector = MyVector;
โ ๊ฐ ์์, ๊ฐ ๊ฐ์ ธ์ฌ ์ ์์ โ ์ํํด์ผ ํ ๋์์ด ์๋ค๊ณ C++์ ์๋ฆด ๋ฟ
โ ํํ์ ์๋
- GetActorLocation()
๐จC++ ์์
๐จ
void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Move platform forwards
// Get current location
FVector CurrentLocation = GetActorLocation();
// Add vector to that location
CurrentLocation.X = CurrentLocation.X + 1;
// Set the location
SetActorLocation(CurrentLocation);
...
}
๐์๋ & ๋ธํ ํ์
๐จUsing DeltaTime
- ์ธ๋ฆฌ์ผ์์ ๋ธํ ํ์์ ์ด์ฉํ๋ฉด,
๊ฐ ํ๋ ์์ด ์คํ๋๋ ๋ฐ ์๊ฐ์ด ์ผ๋ง๋ ๊ฑธ๋ฆฌ๋์ง ์ ์ ์์
- ์ด๋ค ๊ฐ์ ๋ธํ ํ์์ผ๋ก ๊ณฑํ๋ฉด, ๊ฒ์ ํ๋ ์๋ฅ ์ด ๋
๋ฆฝ์ ์ผ๋ก ๋ณํจ
- ์ฆ, ๋น ๋ฅธ ์ปดํจํฐ์ ๋๋ฆฐ ์ปดํจํฐ์์ ๊ฒ์์ด ๋์ผํ๊ฒ ๋์ํ๊ฒ ๋จ
CurrentLocation = CurrentLocation + (PlatformVelocity * DeltaTime);
๐๋ฒ์ ์ง์ ์ฐ์ฐ์
- ํ๋ ์ด๊ฐ ์์๋ ์ดํ ์ผ๋ง๋ ์ด๋ํ๋์ง ํ์ธํ๋ ๋ฒ
1) ๋ ๋ฒกํฐ์ ์ฐจ์ด โ ๋ ์์น์ ๊ฑฐ๋ฆฌ๋ฅผ ์์๋ด์ผ ํจ
2) ์ด๋์ ์์ํ๋์ง
๐์ฉ์ด ์ ๋ฆฌ
- ๋ฒ์ ์ง์ ์ฐ์ฐ์ (::)
: ๋๋ธ ์ฝ๋ก ์ฐ์ฐ์, ์ ์ฐ์ฐ์์ ๋ง์ฐฌ๊ฐ์ง๋ก ํด๋์ค ์์ ๋ณผ ์ ์๊ฒ ํด์ค
: ํด๋์ค์ ์ธ์คํด์ค ๋ด๋ถ๋ฅผ ๋ณผ ์ ์๊ฒ ํด์ค
ex) void AMovingPlatform::Tick(float DeltaTime)
โ Tick ํจ์๋ MovingPlatform ํด๋์ค์ ์ํ๋ค๋ ๊ฑด ์๊ณ ์์ง๋ง ์ด ํด๋์ค์ ํน์ ์ธ์คํด์ค์ ์ํ์ง๋ ์์
โ MovingPlatform์ ๋ชจ๋ ์ธ์คํด์ค๋ ๊ฐ์ Tick ํจ์ ๊ตฌํ์ ๊ณต์ ํจ(ํด๋์ค์ฌ์)
โโ Tick ํจ์๊ฐ MovingPlatform ํด๋์ค์ ์ํ๋ค๋ ๊ฒ์ ๋ปํจ
๐จC++ ์์
๐จ
- ์ฒ์ ์์น์ ํ์ฌ ์์น์ ๊ฑฐ๋ฆฌ ์ฐจ์ด ๊ณ์ฐํ๋ ๊ธฐ๋ฅ ์ถ๊ฐํ๊ธฐ
- ๊ฑฐ๋ฆฌ ์ฐจ์ด ๊ฐ์ DistanceMoved์ ์ ์ฅ
DistanceMoved = FVector::Dist(StartLocation, CurrentLocation);
๐ซIf ๋ฌธ์ฅ
- If ๋ฌธ == C++์ ๋ถ๊ธฐ๋ฌธ
โ ์ด๋ฅผ ํ์ฉํ์ฌ ํ๋ซํผ์ด ์๋ค๋ก ์์ง์ด๊ฒ ๋ง๋ค๊ธฐ
๐จC++ ์์
๐จ
- ํ๋ซํผ์ด ์๋ค๋ก ์์ง์ด๋ ๊ฒ์ ๋ณผ ์ ์์
float DistanceMoved = FVector::Dist(StartLocation, CurrentLocation);
// Reverse direction of motion if gone too far
if (DistanceMoved > MovedDistance)
{
PlatformVelocity = -PlatformVelocity;
}
-
๊ทธ๋ฌ๋, ์์์ ์ ์
๋ฐ์ดํธํ์ง ์์ ํ๋ซํผ์ด ์์ ๋ฐฉํฅ์ผ๋ก ์ด๋, ๊ทธ๋ฐ ๋ค์ ๋ฐ๋๋ก ์์์ ์ ์ง๋ ๊ฐ์ ๊ฑฐ๋ฆฌ๋ฅผ ์ด๋ํจ
-
์์์ ์ ์
๋ฐ์ดํธํ๋ ๊ธฐ๋ฅ์ ๋ฃ์ ์ฝ๋ โผ
float DistanceMoved = FVector::Dist(StartLocation, CurrentLocation);
// Reverse direction of motion if gone too far
if (DistanceMoved > MovedDistance)
{
PlatformVelocity = -PlatformVelocity;
StartLocation = CurrentLocation;
}
- ์์์ ์ ์ง๋์น์ง ์๊ณ ํญ์ ๊ฐ์ ํฌ๊ธฐ ๋งํผ ๋๋์๊ฐ์ ์์์ ์ผ๋ก ๊ฐ
๐ฅ๋ฉค๋ฒ ํจ์ ์ฌ์ฉ
Back and Forth
๐จC++ ์์
๐จ
- ์์ ๊ทธ๋ฆผ์ผ๋ก ํํํ ๊ฐ๋
์ ํ์ฉํ์ฌ ์์์ ๊ณผ ๋์ ์ ์ง๋์น์ง ์๊ณ
ํ๋ซํผ์ด ์์ง์ด๋๋ก ํด์ฃผ๊ธฐ
if (DistanceMoved > MovedDistance)
{
FVector MoveDirection = PlatformVelocity.GetSafeNormal();
StartLocation = StartLocation + MoveDirection * MovedDistance;
SetActorLocation(StartLocation);
PlatformVelocity = -PlatformVelocity;
}
๐ฅ๋ธ๋ฃจํ๋ฆฐํธ์ ํ์ ํด๋์ค
๋ธ๋ฃจํ๋ฆฐํธ ๋ฒ ์ด์ค ํด๋์ค ์์ฑ ๋ฐฉ๋ฒ
- MovingPlatform ์ฐํด๋ฆญ
โ 'MovingPlatform ๊ธฐ๋ฐ ๋ธ๋ฃจํ๋ฆฐํธ ํด๋์ค ์์ฑ' ์ ํ
โ PlatformVelocity๋ z์ถ 200, MovedDistance๋ 500์ผ๋ก ์ค์
โ ๋ชจ์์ ๋๊ธฐ๋ฅ ๋ชจ์์ผ๋ก ํด์ฃผ๊ธฐ
โโ ์คํํด๋ณด๋ฉด ๋๊ธฐ๋ฅ์ด ์์๋๋ก ์์ง์ด๋ ๊ฒ์ ๋ณผ ์ ์์

๐ฑโ๐์บ๋ฆญํฐ ์ถฉ๋ ๊ฐ์ ์ ์ฉ
- 'MovingPlatform ๊ธฐ๋ฐ ๋ธ๋ฃจํ๋ฆฐํธ ํด๋์ค ์์ฑ' โ BP_PushingCube ์์ฑ
์์ฑํ BP_PushingCube๋ฅผ ๋ทฐํฌํธ์ ์ถ๊ฐํด์ ํ๋ ์ด์ด๋ฅผ ํ๋ธ ์์ ๋๋๋ฉด ์ด์ํ๊ฒ ๋๋ ๊ฒ์ ๋ณผ ์ ์์

- ํ๋ ์ด์ด์ ๋ธ๋ฃจํ๋ฆฐํธ์ MoveUpdateComponent ์ถ๊ฐํ์ฌ ์ถฉ๋ ๊ฐ์ ์ ์ฉํ๊ธฐ

- ์์ ๊ฒฐ๊ณผ โผ
