[UE5 / C++] ๐Ÿ’จ ์•กํ„ฐ ์›€์ง์ด๊ธฐ (2)

Singery00ยท2024๋…„ 4์›” 21์ผ
0

UE5 C++

๋ชฉ๋ก ๋ณด๊ธฐ
7/20
post-thumbnail

๊ฐœ์š”

๐Ÿ’ก C++๋กœ ์•กํ„ฐ์˜ Transform์„ ์กฐ์ž‘ํ•ด๋ณด์ž.

C++์„ ์‚ฌ์šฉํ•ด์„œ ๋ ˆ๋ฒจ์— ๋ฐฐ์น˜ํ•œ ์•กํ„ฐ๋ฅผ ์›€์ง์—ฌ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. C++ ์ฝ”๋“œ๋กœ ์•กํ„ฐ ์ƒ์„ฑ
2. ์•กํ„ฐ Transfrom ์กฐ์ •
3. ์•กํ„ฐ ํšŒ์ „ ๊ฐœ์„ 

๋ณธ๋ก 

์ƒ์„ฑํ•œ ์•กํ„ฐ์˜ Transform์„ ์กฐ์ ˆํ•ด์„œ ๋ ˆ๋ฒจ์—์„œ ๊ฐ€์ง€๊ณ  ๋†€์•„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด๋ฅผ ์‘์šฉํ•˜๋ฉด ์žฌ๋ฐŒ๋Š” ์žฅ์• ๋ฌผ๋“ค์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค.

์•กํ„ฐ ์›€์ง์ด๊ธฐ

๋ณ€์ˆ˜ ๋ฐ ํ•จ์ˆ˜ ์„ ์–ธ

์šฐ์„  ํ˜„์žฌ ์•กํ„ฐ์˜ ์œ„์น˜๋ฅผ ๋ฐ›์„ FVector๋ณ€์ˆ˜์™€, ์ด๋™์†๋„๋ฅผ ์ €์žฅํ•  FVector๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋งŒ๋“ค์–ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ ์•กํ„ฐ์˜ ์›€์ง์ž„์„ ๊ตฌํ˜„ํ•  Moveํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

public:
	UPROPERTY(VisibleAnywhere,Category=MyValue)
	FVector currentLocation;

	UPROPERTY(EditAnywhere, Category = MyValue)
	FVector moveSpeed;
    
    void Move(float DeltaTime);

Move() ํ•จ์ˆ˜ ์ •์˜

currentLocation์€ ์•กํ„ฐ์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
๊ทธ ํ›„ moveSpeed์— DeltaTime์„ ๊ณฑํ•œ ๊ฐ’์„ currentLocation์— ๋”ํ•ด์ค€ ํ›„,
๊ทธ ๊ฐ’์„ ์ƒˆ๋กญ๊ฒŒ ์ ์šฉํ•ด์ฃผ๋ฉด ์•กํ„ฐ์˜ ์œ„์น˜๊ฐ€ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.

void AMyActor::Move(float DeltaTime)
{
	currentLocation = GetActorLocation();

	currentLocation += moveSpeed * DeltaTime;

	SetActorLocation(currentLocation);
}

Tick()์—์„œ Move()ํ˜ธ์ถœ

Tick์—์„œ Move()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  DeltaTime์„ ์ธ์ž๋กœ ๋„˜๊ฒจ ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	Move(DeltaTime);
}

โ—์ €์žฅ ํ›„ ์ปดํŒŒ์ผโ— ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ moveSpeed์˜ ๊ฐ’์— ๋”ฐ๋ผ ์•กํ„ฐ์˜ ์œ„์น˜๊ฐ€ ๋ณ€ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•กํ„ฐ ํšŒ์ „ํ•˜๊ธฐ

๋ณ€์ˆ˜ ๋ฐ ํ•จ์ˆ˜ ์„ ์–ธ

์•กํ„ฐ ์ด๋™๊ณผ ๊ฐ™์ด ํšŒ์ „๊ฐ’์„ ์ €์žฅํ•˜๊ณ , ์ „๋‹ฌํ•  ๋ณ€์ˆ˜๋ฅผ FRotator๋กœ ํ•˜๋‚˜์”ฉ ๋งŒ๋“ค์–ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

public:
	// ์ƒ๋žต

	UPROPERTY(VisibleAnywhere, Category = MyValue)
	FRotator currentRotation;

	UPROPERTY(EditAnywhere, Category = MyValue)
	FRotator rotateSpeed;

	void Rotate(float DeltaTime);

};

Rotation()ํ•จ์ˆ˜ ์ •์˜

Rotation()๋„ Move()์™€ ๋™์ผํ•œ ๋กœ์ง์œผ๋กœ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

void AMyActor::Rotate(float DeltaTime)
{
	currentRotation = GetActorRotation();

	currentRotation += rotateSpeed * DeltaTime;

	SetActorRotation(currentRotation);
}

Tick()์—์„œ Rotation()ํ˜ธ์ถœ

Tick()ํ•จ์ˆ˜์—์„œ DeltaTime์„ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๊ณ ,
Move()์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•ด์ค๋‹ˆ๋‹ค.

void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	Move(DeltaTime);
	Rotate(DeltaTime);
}

โ—์ €์žฅ ํ›„ ์ปดํŒŒ์ผโ— ํ›„ ๊ฐ’์— ๋”ฐ๋ผ ์•กํ„ฐ๊ฐ€ ํšŒ์ „ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋งˆ๋ฌด๋ฆฌ

์ž ๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ๋ฐฉ์‹์œผ๋กœ ํšŒ์ „์„ ๊ตฌํ˜„ํ•˜๋‹ˆ ์•กํ„ฐ์˜ Roll์ด ์ตœ๋Œ€ 180๋„๊นŒ์ง€๋งŒ ํšŒ์ „์„ ํ•˜๊ณ  ๋ฒ„๋ฒ… ๊ฑฐ๋ฆฌ๋Š” ์ด์Šˆ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์— ๋Œ€ํ•œ ์„ค๋ช…๊ณผ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

1. C++ ์ฝ”๋“œ๋กœ ์•กํ„ฐ ์ƒ์„ฑ
2. ์•กํ„ฐ Transfrom ์กฐ์ •
3. ์•กํ„ฐ ํšŒ์ „ ๊ฐœ์„ 

profile
๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์–ด๋ณด์ž

0๊ฐœ์˜ ๋Œ“๊ธ€