10c++呵呵老师【uec++子弹销毁物体】

创建一个漂浮物,放在场景中,子弹击中就会消失。

首先是添加静态网格体变量

	UPROPERTY(VisibleAnywhere , Category = "Static")
	UStaticMeshComponent* VisualMesh;

添加检查actor的函数声明

UFUNCTION()
	void CheckActor( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

在源文件中引用头文件

#include "FloatingActor.h"
//#include"Components/PrimitiveComponent.h"
#include"Components/StaticMeshComponent.h"
#include"FPSGame/FPSGameProjectile.h"

  然后在构造函数中进行初始化,并设置重叠事件的激活

// 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;
	VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));


    //actor的重叠事件的激活!!!!!!!!!!!!
	VisualMesh->OnComponentBeginOverlap.AddDynamic(this , &AFloatingActor::CheckActor);
	VisualMesh->SetupAttachment(RootComponent);



	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

	if (CubeVisualAsset.Succeeded())
	{
		VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
		VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
	}

 定义成员函数

void AFloatingActor::CheckActor( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	//将碰撞漂浮物的另一个actor的指针转化为子弹类型的指针
	AFPSGameProjectile * Projectile = Cast<AFPSGameProjectile>(OtherActor);
	//如果转化失败,说明不是子弹碰撞的漂浮物
	if (Projectile == nullptr)
	{
		return;
	}
	//否则就是子弹击中了漂浮物
	else
	{
		this->Destroy();
	}
}

打开编辑器,点击场景中的漂浮物

设置碰撞预设

 

测试 成功

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无情的阅读机器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值