Playerの作成
弾丸の作成
弾丸の作成です。
以下の手順で作成します。
- c++ でアクターを作成する
- アクターをBPにする
- メッシュとコンポーネントを割り当てる
- ACppPlayerControllerから弾丸を発射する
アクターを作成する
全文 bullet00.h
/**
* @file bullet00.h
* @brief 弾丸
* @author inuvatico
* @date 2020/11/08
* @version 1.0
* @copyright 2018 inuvatico
* Released under the MIT license.
* see https://opensource.org/licenses/MIT
* @par (new/Add/Change : 2020/10/17)
*
*/
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Bullet00.generated.h"
class UHitComponent;
UCLASS()
class ABullet00 : public AActor
{
GENERATED_BODY()
public:
static AActor*Create(const UWorld *World, FVector pos, FRotator rot);
public:
ABullet00();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
private:
float Time;
UHitComponent* HitComponent;
};
全文 bullet00.cpp
/**
* @file bullet00.cpp
* @brief 弾丸
* @author inuvatico
* @date 2020/11/08
* @version 1.0
* @copyright 2018 inuvatico
* Released under the MIT license.
* see https://opensource.org/licenses/MIT
* @par (new/Add/Change : 2020/10/17)
*
*/
#include "Components/SphereComponent.h"
#include <uty/AttakDamageBase.h>
#include "bullet00.h"
//-----------------------------------------------------
//!弾丸の作成
//-----------------------------------------------------
AActor*ABullet00::Create(const UWorld *World, FVector pos, FRotator rot ) {
AActor* Actor = nullptr;
const FString path = "/Game/app/chr/bullet/Bullet00_00C.Bullet00_00C_C";
TSubclassOf<class AActor> sc = TSoftClassPtr<AActor>(FSoftObjectPath(*path)).LoadSynchronous();
if (sc != nullptr) {
Actor = World->GetWorld()->SpawnActor<AActor>(sc);
Actor->SetActorLocation(pos, false, nullptr, ETeleportType::ResetPhysics);
Actor->SetActorRotation(rot);
}
return Actor;
}
//-----------------------------------------------------
// コンストラクタ
//-----------------------------------------------------
ABullet00::ABullet00()
{
PrimaryActorTick.bCanEverTick = true;
}
//-----------------------------------------------------
// BeginPlay
//-----------------------------------------------------
void ABullet00::BeginPlay()
{
Super::BeginPlay();
Time = 5.0f;
HitComponent = FindComponentByClass<UHitComponent>();
}
//-----------------------------------------------------
// Tick
//-----------------------------------------------------
void ABullet00::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
bool bDestroy = false;
//移動処理
{
FRotator rot = GetActorRotation();
float angl = FMath::DegreesToRadians(rot.Yaw);
FVector vec(0, 0, 0);
vec.X = cos(angl);
vec.Y = sin(angl);
vec *= DeltaTime * 100.0f * 4;
vec += GetActorLocation();
SetActorLocation(vec, true, nullptr, ETeleportType::TeleportPhysics);
}
Time -= DeltaTime;
if (Time <= 0) {
bDestroy = true;
}
if (HitComponent) {
if (HitComponent->HitAtb) {
bDestroy = true;
}
}
if (bDestroy) {
Destroy();
}
}
- Create() : 弾丸アクターを作成します。
リソースまでのパス(/Game/app/chr/bullet/Bullet00_00C.Bullet00_00C_C)を利用してスポーンします。 - BeginPlay(): 衝突を検知するコンポーネントを取得します。また、弾丸のライフタイムを設定しています。
- Tick():移動と弾丸の破棄を行います。弾丸は一定時間を過ぎるか何かにぶつかると破棄されます。
アクターをBPにする
C++クラスからBullet00
選び
ブループリントクラスを作成します。
↓
メッシュとコンポーネントを割り当てる
BPを開いてコンポーネントの追加から適当なMesh
とUHitComponent
を追加する
&
Tagの設定
衝突検出をするコンポーネント('Mesh')にTag"NotifiyCollision"
を設定する
物理設定とコリジョン設定
必要に応じて物理設定とコリジョン設定を行う
ACppPlayerControllerから弾丸を発射する
ACppPlayerController.cppにbullet/bullet00.hをインクルードし、
ABullet00::Create()
を呼び出します。
//-------------------------------------------------
//! 攻撃(SHOOT)
//-------------------------------------------------
void ACppPlayerController::ActAttack1(CallType type)
{
if (CallType::INIT == type) {
MyActor->AnmStateComponent->SetAnmIndex(UAnmIndex::ANMID_ATTACK1);
}if (CallType::ACT == type) {
//射撃1
if (MyActor->AnmStateComponent->AnmEventBit& (1 << static_cast<int>(UAnmEvent::ST_SHOOT1))) {
auto pos = MyActor->GetMesh()->GetSocketLocation("Muzzle_01");
auto rot = MyActor->GetActorRotation();
ABullet00::Create(MyActor->GetWorld(), pos,rot);
MyActor->AnmStateComponent->ClearEventNo((int)UAnmEvent::ST_SHOOT1);
}
//射撃2
if (MyActor->AnmStateComponent->AnmEventBit& (1 << static_cast<int>(UAnmEvent::ST_SHOOT2))) {
MyActor->AnmStateComponent->ClearEventNo((int)UAnmEvent::ST_SHOOT2);
auto pos = MyActor->GetMesh()->GetSocketLocation("Muzzle_02");
auto rot = MyActor->GetActorRotation();
ABullet00::Create(MyActor->GetWorld(), pos, rot);
}
if (MyActor->AnmStateComponent->AnmEventBit& (1 << static_cast<int>(UAnmEvent::ST_END))) {
SetAction(ActType::IdleRun);
return;
}
}
}
位置はGetSocketLocation()
で向きはMyActor->GetActorRotation();
で取得しABullet00::Create()
に渡しています。
これで射撃ができるようになりました。