UE C++ 代理碰撞事件 设置碰撞类型

一.在Actor中声明碰撞BOX组件

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
		class UBoxComponent* MyBox;

在Actor以这样的形式实现代理绑定,在BeginPlay()里。

MyBox->OnComponentBeginOverlap.AddDynamic();

转到OnComponentBeginOverlap定义

再转到这个代理的定义

/** Delegate for notification of start of overlap with a specific component */
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_SixParams( FComponentBeginOverlapSignature, UPrimitiveComponent, OnComponentBeginOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult);

这里是6个参数,从后向前数六个参数。将中间的“,”去掉,作为调用函数的参数。其余两个绑定也是类似的思路。

	UFUNCTION()
	void BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	UFUNCTION()
	void EndOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); //四个参数
	// FComponentEndOverlapSignature, UPrimitiveComponent, OnComponentEndOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex
	UFUNCTION()
	void HitOverlaoFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
	//UPrimitiveComponent*, HitComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, FVector, NormalImpulse, const FHitResult&, Hit

声明后在cpp中实现

void AMyActor::BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("BeginOverLapEvent is Success"));
}

void AMyActor::EndOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("EndOverlapEvent is Success"));
}

void AMyActor::HitOverlaoFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("HitOverlapEvent is Success"));
}

回调的函数完成

最上面的代理绑定完整版如下:

	MyBox->OnComponentBeginOverlap.AddDynamic(this,&AMyActor::BeginOverlapFunction);
	MyBox->OnComponentEndOverlap.AddDynamic(this,&AMyActor::EndOverlapFunction);
	MyBox->OnComponentHit.AddDynamic(this,&AMyActor::HitOverlaoFunction);

一般这里有报错,有可能就是代理调用函数的参数个数弄错了。

这里测试碰撞前 碰撞后的代理函数

设置碰撞,就可以一直调用 碰撞中的代理函数

设置碰撞类型 检测等的设置如下:

早构造函数中

	//碰撞设置
	//MyBox->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	MyBox->SetCollisionEnabled(ECollisionEnabled::QueryOnly);   //查询碰撞
	//MyBox->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly); //物理碰撞
	//MyBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); // 物理查询碰撞都有
	//MyBox->SetCollisionEnabled(ECollisionEnabled::ProbeOnly); // 物理查询碰撞都有
	//MyBox->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe); // 物理查询碰撞都有
	//设置碰撞类型
	//MyBox->SetCollisionObjectType(ECC_WorldStatic); //世界静态
	MyBox->SetCollisionObjectType(ECC_WorldDynamic); //世界动态的设置
	//MyBox->SetCollisionObjectType(ECC_Pawn);  //人形
	//MyBox->SetCollisionObjectType(ECC_PhysicsBody); //物理人体
	//MyBox->SetCollisionObjectType(ECC_Vehicle); //载具
	//MyBox->SetCollisionObjectType(ECC_Destructible); //可破碎物体的碰撞
	//设置碰撞响应
	//MyBox->SetCollisionResponseToAllChannels(ECR_Block);  //对所有的通道进行设置响应为Block阻挡
	MyBox->SetCollisionResponseToAllChannels(ECR_Overlap); //对所有的通道进行设置响应为Overlap重叠
	//MyBox->SetCollisionResponseToAllChannels(ECR_Ignore); //对所有的通道进行设置响应为Ignore忽略
	单个
	//MyBox->SetCollisionResponseToChannel(ECC_Pawn,ECR_Overlap); //对Pawn通道进行设置响应为Block阻挡
	//MyBox->SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Block); //对WorldStatic通道进行设置响应为Block阻挡
	//MyBox->SetCollisionResponseToChannel(ECC_WorldDynamic, ECR_Ignore);
	MyBox->SetBoxExtent(FVector(64,64,64));

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值