์ธ๋ฆฌ์ผ Rotating Raycast
ํ๊ทธ: C++, Unreal, ์ธ๋ฆฌ์ผ
์นดํ ๊ณ ๋ฆฌ: UnRealStudy
๐ ํ์ ํ๋ ๋ ์ด์บ์คํธ
๐ Raycast
ํด๋น ๊ธฐ๋ฅ์ ์ฌ์ฌ์ฉ ํ๊ธฐ ์ํด ์ปดํฌ๋ํธ๋ก ๋ง๋ ๋ค.
1
2
3
4
5
6
7
8
9
10
11
class BLOGPROJECT_API URaycast : public USceneComponent
{
public:
UPROPERTY(EditAnywhere)
float Distance;
FVector StartPos;
FVector Forward;
FCollisionQueryParams Params;
FCollisionObjectQueryParams ObjectParams;
FHitResult HitResult;
};
FCollisionQueryParams
์ FCollisionObjectQueryParams
๋ ์ถฉ๋์ฒด ๊ฐ์ ์ค์ ์ ํ๋ ๊ตฌ์กฐ์ฒด์ด๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void URaycast::BeginPlay()
{
Super::BeginPlay();
StartPos = GetComponentLocation();
// ์์ ์ ์ถฉ๋์์ ์ ์ธ
Params.AddIgnoredActor(GetOwner());
// PhysicsBody ๋ฅผ ์ถฉ๋ํ๋๋ก ์ถ๊ฐ
ObjectParams.AddObjectTypesToQuery(ECollisionChannel::ECC_PhysicsBody);
}
void URaycast::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
Forward = GetForwardVector() * Distance;
if(GetWorld()->LineTraceSingleByObjectType(HitResult, StartPos, StartPos + Forward, ObjectParams, Params))
{
DrawDebugLine(GetWorld(), StartPos, StartPos + Forward, FColor::Green, false, 0.01f, 0, 5.0f);
}
else
{
DrawDebugLine(GetWorld(), StartPos, StartPos + Forward, FColor::Red, false, 0.01f, 0, 5.0f);
}
}
๋ค์๊ณผ ๊ฐ์ด ์ปดํฌ๋ํธ๋ฅผ ์ถ๊ฐํด์ฃผ๊ณ Distance ๋ฅผ ์ค์ ํ๋ค.
Raycast ๊ฐ ObjectType ์ PhysicsBody ๋ก ์ค์ ํ ํ๋ธ์ ์ถฉ๋ํ๋ฉด
์์ ์ฌ์ง๊ณผ ๊ฐ์ด ๋์ํ๋ค.
์ด์ ํ์ ์ ์ฃผ์ด ๋ ์ด๋์ ๊ฐ์ด ์๋ํ๋๋ก ๋ง๋ค์ด๋ณด์.
๐ Rotate
1
2
3
public:
UPROPERTY(EditAnywhere)
FRotator RotateVelocity = FRotator(0, 0, 0);
ํค๋์ ๋ค์๊ณผ ๊ฐ์ด ์ถ๊ฐํ๊ณ
1
2
3
4
5
6
7
8
9
10
void URaycast::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
...
Rot = GetOwner()->GetActorRotation();
Rot = Rot + RotateVelocity * DeltaTime;
GetOwner()->SetActorRotation(Rot);
...
}
cpp ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ์ถ๊ฐํ๊ณ ์๋์์ผ๋ณด์.
๋๊ธ๋จ๊ธฐ๊ธฐ