/ **
*このアクターにダメージを与えます。
* @see https://www.unrealengine.com/blog/damage-in-ue4
* @paramDamageAmount適用するダメージの量
* @paramDamageEvent受けたダメージを完全に説明するデータパッケージ。
* @paramEventInstigator損傷の原因となったコントローラー。
* @param DamageCauserダメージを直接引き起こしたアクター(爆発した発射物、着地した岩など)
* @ return実際に適用されたダメージの量。
* /
/**
* Apply damage to this actor.
* @see https://www.unrealengine.com/blog/damage-in-ue4
* @param DamageAmount How much damage to apply
* @param DamageEvent Data package that fully describes the damage received.
* @param EventInstigator The Controller responsible for the damage.
* @param DamageCauser The Actor that directly caused the damage (e.g. the projectile that exploded, the rock that landed on you)
* @return The amount of damage actually applied.
*/
virtual float TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser);
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Gun.generated.h"
UCLASS()
class SIMPLESHOOTERCP2_API AGun : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AGun();
void PullTrigger();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY(VisibleAnyWhere)
USceneComponent* Root;
UPROPERTY(VisibleAnyWhere)
USkeletalMeshComponent* Mesh;
UPROPERTY(EditAnyWhere)
UParticleSystem* MuzzleFlash;
UPROPERTY(EditAnyWhere)
UParticleSystem* ImpactEffect;
UPROPERTY(EditAnyWhere)
float MaxRange = 1000;
};
Gun.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Gun.h"
#include "Components/SkeletalMeshComponent.h"
#include "Kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"
// Sets default values
AGun::AGun()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(Root);
}
void AGun::PullTrigger()
{
UE_LOG(LogTemp,Warning,TEXT("You have been shot!! "));
//MuzzleFlashSocket
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, Mesh, TEXT("MuzzleFlashSocket"));
APawn* OwnerPawn = Cast<APawn>(GetOwner());
if(OwnerPawn==nullptr)
{
return;
}
AController* OwnerController= OwnerPawn ->GetController();
if(OwnerController==nullptr)
{
return;
}
FVector Location= OwnerPawn->GetActorLocation();
FRotator Rotation;
OwnerController->GetPlayerViewPoint( Location, Rotation );
FVector End = Location + Rotation.Vector() * MaxRange;
// TOTO: LineTrace
float FOVDeg=90;
float Scale=2.f;
FColor const& Color=FColor::Blue;
bool bPersistentLines=true;
float LifeTime=-1.f;
uint8 DepthPriority = 0;
//DrawDebugCamera(GetWorld(), Location, Rotation, FOVDeg, Scale, Color, bPersistentLines, LifeTime, DepthPriority);
float Size2 = 20.0;
FColor const& Color2= FColor::Green;
bool bPersistentLines2 = true;
float LifeTime2 = -1.f;
uint8 DepthPriority2 = 0;
//DrawDebugPoint(GetWorld(), Location,Size2 ,Color2 , bPersistentLines2, LifeTime2, DepthPriority2);
//DrawDebugPoint(GetWorld(), Location,Size2 ,Color2 , bPersistentLines2);
//DrawDebugDirectionalArrow(GetWorld(), Location, End, 3.0, FColor::Blue, bPersistentLines);
FHitResult Hit;
ECollisionChannel TraceChannel = ECollisionChannel::ECC_GameTraceChannel1;
bool bSuccess = GetWorld()->LineTraceSingleByChannel(Hit,Location,End,TraceChannel);
if(bSuccess==true)
{
UE_LOG(LogTemp,Warning,TEXT("HitWall!! "));
//DrawDebugPoint(GetWorld(), Hit.Location,40.0 ,FColor::Red , bPersistentLines2);
FVector ShotDirection = -Rotation.Vector();
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, Hit.Location,ShotDirection.Rotation() );
}
//DrawDebugPoint(GetWorld(), End,Size2 ,FColor::Red , bPersistentLines2);
}
// Called when the game starts or when spawned
void AGun::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGun::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
/ **
*特定のチャネルを使用して世界に対して光線をトレースし、ブロッキングヒットが見つかった場合に戻ります。
* @paramStart光線の開始位置
* @paramEnd光線の終了位置
* @paramTraceChannelこの光線が含まれる「チャネル」。ヒットするコンポーネントを決定するために使用されます。
* @paramParamsトレースに使用される追加のパラメーター
*このトレースに使用される@paramResponseParam ResponseContainer
* @ returnブロッキングヒットが見つかった場合はTRUE
* /
// LINE TRACE
/**
* Trace a ray against the world using a specific channel and return if a blocking hit is found.
* @param Start Start location of the ray
* @param End End location of the ray
* @param TraceChannel The 'channel' that this ray is in, used to determine which components to hit
* @param Params Additional parameters used for the trace
* @param ResponseParam ResponseContainer to be used for this trace
* @return TRUE if a blocking hit is found
*/
bool LineTraceTestByChannel(const FVector& Start,const FVector& End,ECollisionChannel TraceChannel, const FCollisionQueryParams& Params = FCollisionQueryParams::DefaultQueryParam, const FCollisionResponseParams& ResponseParam = FCollisionResponseParams::DefaultResponseParam) const;
LineTraceSingleByChannel
/ **
*特定のチャネルを使用して世界に対して光線をトレースし、最初のブロッキングヒットを返します
* @paramOutHit最初のブロッキングヒットが見つかりました
* @paramStart光線の開始位置
* @paramEnd光線の終了位置
* @paramTraceChannelこの光線が含まれる「チャネル」。ヒットするコンポーネントを決定するために使用されます。
* @paramParamsトレースに使用される追加のパラメーター
*このトレースに使用される@paramResponseParam ResponseContainer
* @ returnブロッキングヒットが見つかった場合はTRUE
* /
/**
* Trace a ray against the world using a specific channel and return the first blocking hit
* @param OutHit First blocking hit found
* @param Start Start location of the ray
* @param End End location of the ray
* @param TraceChannel The 'channel' that this ray is in, used to determine which components to hit
* @param Params Additional parameters used for the trace
* @param ResponseParam ResponseContainer to be used for this trace
* @return TRUE if a blocking hit is found
*/
bool LineTraceSingleByChannel(struct FHitResult& OutHit,const FVector& Start,const FVector& End,ECollisionChannel TraceChannel,const FCollisionQueryParams& Params = FCollisionQueryParams::DefaultQueryParam, const FCollisionResponseParams& ResponseParam = FCollisionResponseParams::DefaultResponseParam) const;
/ **
*特定のチャネルを使用して世界に対して光線をトレースし、最初のブロッキングヒットを返します
* @paramOutHit最初のブロッキングヒットが見つかりました
* @paramStart光線の開始位置
* @paramEnd光線の終了位置
* @paramTraceChannelこの光線が含まれる「チャネル」。ヒットするコンポーネントを決定するために使用されます。
* @paramParamsトレースに使用される追加のパラメーター
*このトレースに使用される@paramResponseParam ResponseContainer
* @ returnブロッキングヒットが見つかった場合はTRUE
* /
/**
* Trace a ray against the world using a specific channel and return the first blocking hit
* @param OutHit First blocking hit found
* @param Start Start location of the ray
* @param End End location of the ray
* @param TraceChannel The 'channel' that this ray is in, used to determine which components to hit
* @param Params Additional parameters used for the trace
* @param ResponseParam ResponseContainer to be used for this trace
* @return TRUE if a blocking hit is found
*/
bool LineTraceSingleByChannel(struct FHitResult& OutHit,const FVector& Start,const FVector& End,ECollisionChannel TraceChannel,const FCollisionQueryParams& Params = FCollisionQueryParams::DefaultQueryParam, const FCollisionResponseParams& ResponseParam = FCollisionResponseParams::DefaultResponseParam) const;
Gun.hはこんなんで
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Gun.generated.h"
UCLASS()
class SIMPLESHOOTERCP2_API AGun : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AGun();
void PullTrigger();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY(VisibleAnyWhere)
USceneComponent* Root;
UPROPERTY(VisibleAnyWhere)
USkeletalMeshComponent* Mesh;
UPROPERTY(EditAnyWhere)
UParticleSystem* MuzzleFlash;
UPROPERTY(EditAnyWhere)
float MaxRange = 1000;
};
Gun.cppはこんな感じになった
// Fill out your copyright notice in the Description page of Project Settings.
#include "Gun.h"
#include "Components/SkeletalMeshComponent.h"
#include "Kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"
// Sets default values
AGun::AGun()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(Root);
}
void AGun::PullTrigger()
{
UE_LOG(LogTemp,Warning,TEXT("You have been shot!! "));
//MuzzleFlashSocket
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, Mesh, TEXT("MuzzleFlashSocket"));
APawn* OwnerPawn = Cast<APawn>(GetOwner());
if(OwnerPawn==nullptr)
{
return;
}
AController* OwnerController= OwnerPawn ->GetController();
if(OwnerController==nullptr)
{
return;
}
FVector Location= OwnerPawn->GetActorLocation();
FRotator Rotation;
OwnerController->GetPlayerViewPoint( Location, Rotation );
FVector End = Location + Rotation.Vector() * MaxRange;
// TOTO: LineTrace
float FOVDeg=90;
float Scale=2.f;
FColor const& Color=FColor::Blue;
bool bPersistentLines=true;
float LifeTime=-1.f;
uint8 DepthPriority = 0;
DrawDebugCamera(GetWorld(), Location, Rotation, FOVDeg, Scale, Color, bPersistentLines, LifeTime, DepthPriority);
float Size2 = 20.0;
FColor const& Color2= FColor::Green;
bool bPersistentLines2 = true;
float LifeTime2 = -1.f;
uint8 DepthPriority2 = 0;
//DrawDebugPoint(GetWorld(), Location,Size2 ,Color2 , bPersistentLines2, LifeTime2, DepthPriority2);
DrawDebugPoint(GetWorld(), Location,Size2 ,Color2 , bPersistentLines2);
DrawDebugDirectionalArrow(GetWorld(), Location, End, 3.0, FColor::Blue, bPersistentLines);
FHitResult Hit;
ECollisionChannel TraceChannel = ECollisionChannel::ECC_GameTraceChannel1;
bool bSuuccess = GetWorld()->LineTraceSingleByChannel(Hit,Location,End,TraceChannel);
if(bSuuccess==true)
{
UE_LOG(LogTemp,Warning,TEXT("HitWall!! "));
DrawDebugPoint(GetWorld(), Hit.Location,40.0 ,FColor::Red , bPersistentLines2);
}
DrawDebugPoint(GetWorld(), End,Size2 ,FColor::Red , bPersistentLines2);
}
// Called when the game starts or when spawned
void AGun::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGun::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}