既然神罗天征都做了,那索性把地爆天星也给做了吧。
这里做几个小改动,首先就是将TargetActor的启动方式从Instant改为User Confirm。流程就变为1.start targeting 2.玩家confirm 3.结束targeting并传输数据
第二是给Actor加上tick,因为目标追踪本身是一个Actor,那么它自然可以拥有心跳(tick)。我们期望在玩家确认前可以随时调整技能的释放位置。
其它就是一些技能特效和逻辑的小改动。
先看看最终的效果吧(图片太大了放不进去)
首先还是创建TargetActor派生类,命名为AbilityChibakuTenseiTargetActor。
头文件中和之前做了些改动,需要重写Tick函数。新增了两个Actor对象,一个是用来显示技能释放的位置,这个位置会在Tick当中更新;一个用来作为技能效果,也就是视频中中心的那个球。还多了一个函数,用来获取角色的视线与地面碰撞的位置。
UCLASS()
class GAS_LEARN_API AAbilityChibakuTenseiTargetActor : public AGameplayAbilityTargetActor
{
GENERATED_BODY()
public:
AAbilityChibakuTenseiTargetActor();
virtual void StartTargeting(UGameplayAbility* Ability) override;
virtual void ConfirmTargetingAndContinue() override;
virtual void Tick(float DeltaSeconds) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Ability Settings", meta=(ExposeOnSpawn = true))
float Radius;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Ability Settings", meta=(ExposeOnSpawn = true))
TSubclassOf<AActor> ConePointClass;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Ability Settings", meta=(ExposeOnSpawn = true))
TSubclassOf<AActor> MeteorClass;
// Get Player Looking Point Vector
UFUNCTION(BlueprintCallable, Category="Chikabu Tensei")
bool GetPlayerLookingPoint(FVector& LookingPoint);
protected:
UPROPERTY()
AActor* ConePoint;
UPROPERTY()
AActor* Meteor;
};
构造函数中需要让Actor拥有心跳,所以需要让bCanEverTick=true
AAbilityChibakuTenseiTargetActor::AAbilityChibakuTenseiTargetActor()
{
PrimaryActorTick.bCanEverTick = true;
}
在开始追踪目标后,创建Actor ConePoint用来表示技能的释放位置。
void AAbilityChibakuTenseiTargetActor::StartTargeting(UGameplayAbility* Ability)
{
OwningAbility = Ability;
MasterPC = Cast<APlayerController>(Ability->GetOwningActorFromActorInfo()->GetInstigatorController());
SourceActor = Ability->GetOwningActorFromActorInfo();
ConePoint = GetWorld()->SpawnActor<AActor>(ConePointClass, FVector::ZeroVector, FRotator::ZeroRotator);
}
GetPlayerLookingPoint用来获取视线与地面的碰撞点:
bool AAbilityChibakuTenseiTargetActor::GetPlayerLookingPoint(FVector& LookingPoint)
{
// Get Player View Vector and View Rotation
FVector ViewVector;
FRotator ViewRotation;
if(MasterPC)
MasterPC->GetPlayerViewPoint(ViewVector, ViewRotation);
FCollisionQueryParams QueryParams;
QueryParams.bTraceComplex = false;
if(SourceActor)
{
QueryParams.AddIgnoredActor(SourceActor->GetUniqueID());
}
FHitResult HitResult;
bool TryTrace = GetWorld()->LineTraceSingleByChannel(
HitResult,
ViewVector,
ViewVector + ViewRotation.Vector()*10000.0f,
ECollisionChannel::ECC_Visibility,
QueryParams);
if(TryTrace)
{
LookingPoint = HitResult.ImpactPoint;
}
else
{
LookingPoint = FVector::ZeroVector;
}
return TryTrace;
}
在Tick函数中,首先获取LookLocation,然后更新ConePoint的位置为视线看向的位置。
void AAbilityChibakuTenseiTargetActor::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
FVector LookLocation;
GetPlayerLookingPoint(LookLocation);
ConePoint->SetActorLocation(LookLocation);
}
ConfirmTargetingAndContinue函数基本和上一篇文章里的一样。
唯一的区别就是碰撞检测完成后生成一个代表技能效果的火球Actor,然后也将球放入TargetData中。
void AAbilityChibakuTenseiTargetActor::ConfirmTargetingAndContinue()
{
// ViewLocation是生成碰撞检测的位置
FVector ViewLocation;
GetPlayerLookingPoint(ViewLocation);
TArray<FOverlapResult> OverlapResults;
TArray<TWeakObjectPtr<AActor>> OverlapActors;
FCollisionQueryParams QueryParams;
QueryParams.bTraceComplex = false;
QueryParams.bReturnPhysicalMaterial = false;
if(SourceActor)
{
QueryParams.AddIgnoredActor(SourceActor->GetUniqueID());
}
bool TryOverlap = GetWorld()->OverlapMultiByObjectType(
OverlapResults,
ViewLocation,
FQuat::Identity,
FCollisionObjectQueryParams(ECC_Pawn),
FCollisionShape::MakeSphere(Radius),
QueryParams);
if(TryOverlap)
{
for(FOverlapResult& OverlapResult: OverlapResults)
{
APawn* PawnOverlapped = Cast<APawn>(OverlapResult.Actor);
if(PawnOverlapped)
{
OverlapActors.AddUnique(PawnOverlapped);
}
}
}
FVector MeteorSpawnLocation = ConePoint->GetActorLocation();
MeteorSpawnLocation += ConePoint->GetActorUpVector() * 100.0f;
Meteor = GetWorld()->SpawnActor<AActor>(MeteorClass, MeteorSpawnLocation, ConePoint->GetActorRotation());
ConePoint->Destroy();
OverlapActors.Add(Meteor);
FGameplayAbilityTargetDataHandle TargetData = StartLocation.MakeTargetDataHandleFromActors(OverlapActors);
TargetDataReadyDelegate.Broadcast(TargetData);
}
这里效果不做任何要求,可以随自己喜欢。唯一需要注意的是注意设置Meteor生命周期,让它可以自动销毁。
Cone Point
Meteor
加了个粒子特效,需要设置生命周期为5.0s
在角色蓝图中新建Custom Event,效果是在持续时间内让角色向着Target方向不断移动。
方法是设置一个Timer,在持续时间结束后销毁Timer Handler
这里的Push Timer是在Timer中起效果的事件。它的效果和之前的Push Character Event一样,向着球心施加一个Impluse。
技能效果,其实我认为更好的方法是在BP_Meteor中添加一个Sphere Collision,每隔一段时间对在技能效果内的敌人造成一次GE的instant伤害,但是这次我为了方便就简单弄了,就是玩。
创建GA_ChibakuTensei,释放技能后启动之前实现的Target Actor Task
这里的数据设置要注意,我们选择技能范围为1000。ConePoint Class和Meteror Class为刚才创建的蓝图Actor
获取Data后,其中存储着发生碰撞的Actors和Meteor,Meteor是放在data array的最后一个位置中。向每个目标范围内的Character,启动刚才实现的Custom Event,然后应用技能效果。
其它包括播放动画,音效等等和上一篇文章的实现方法一样,我就不写了,相信你们!
虚幻四-GAS系统实现地爆天星
GAS入门合集