主要是发射函数以及碰撞盒子:
发射函数:
//ACharacter类提供的发射函数
void ACharacter::LaunchCharacter(FVector LaunchVelocity, bool bXYOverride, bool bZOverride)
//UPrimitiveComponent提供的施加力的函数
void UPrimitiveComponent::AddImpulse(FVector Impulse, FName BoneName, bool bVelChange)
参数FVector计算:
//发射的力与角度
LaunchStrength = 1500;
LaunchAngle = 35.0f;
//FVector参数计算
//获取平台的发射方向,以平台的Rotation为方向
FRotator LaunchDirection = GetActorRotation();
//Pitch参数调整发射角度 FRotator (Pitch, Yaw, Roll)分别绕y,z,x旋转
LaunchDirection.Pitch+= LaunchAngle;
//计算FVector
FVector LaunchVelocity = LaunchDirection.Vector() * LaunchStrength;
碰撞事件:
构造函数:
OverlapComp->OnComponentBeginOverlap.AddDynamic(this, &ALaunchPad::OnOverlapBegin);
碰撞函数:
void ALaunchPad::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp,int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
AActor* OtherActor //用于角色碰撞检测
UPrimitiveComponent* OtherComp //用于其他碰撞检测,比如启动重叠事件的方块
完整代码:
头文件部分:
UPROPERTY(VisibleAnywhere, Category = "Components")
class UStaticMeshComponent* LaunchPad;
UPROPERTY(VisibleAnywhere, Category = "Components")
class UBoxComponent* OverlapComp;
UFUNCTION()
void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UPROPERTY(EditInstanceOnly,Category = "Launch")
float LaunchAngle;
UPROPERTY(EditInstanceOnly, Category = "Launch")
float LaunchStrength;
//粒子效果
UPROPERTY(EditAnywhere,Category = "Launch")
class UParticleSystem* LaunchEffect;
//声音
UPROPERTY(EditAnywhere, Category = "Launch")
class USoundBase* LaunchSound;
源文件部分:
//构造函数:
OverlapComp = CreateDefaultSubobject<UBoxComponent>(TEXT("OverlapComp"));
OverlapComp->SetBoxExtent(FVector(75, 75, 50));
RootComponent = OverlapComp;
LaunchPad = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LaunchPad"));
LaunchPad->SetupAttachment(RootComponent);
OverlapComp->OnComponentBeginOverlap.AddDynamic(this, &ALaunchPad::OnOverlapBegin);
LaunchStrength = 1500;
LaunchAngle = 35.0f;
//重叠事件
void ALaunchPad::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
FRotator LaunchDirection = GetActorRotation();
LaunchDirection.Pitch+= LaunchAngle;
FVector LaunchVelocity = LaunchDirection.Vector() * LaunchStrength;
ACharacter* OtherCharacter = Cast<ACharacter>(OtherActor);
if (OtherCharacter)
{
OtherCharacter->LaunchCharacter(LaunchVelocity, true, true);
if (LaunchEffect)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), LaunchEffect, GetActorLocation());
}
if (LaunchSound)
{
UGameplayStatics::PlaySound2D(GetWorld(), LaunchSound);
}
}
else if (OtherComp && OtherComp->IsSimulatingPhysics()) {
OtherComp->AddImpulse(LaunchVelocity, NAME_None, true);
if (LaunchEffect)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), LaunchEffect, GetActorLocation());
}
if (LaunchSound)
{
UGameplayStatics::PlaySound2D(GetWorld(), LaunchSound);
}
}
}