UE4弹射平台LunchPad

主要是发射函数以及碰撞盒子:

发射函数:

//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);
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值