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

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

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

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

相关推荐

  1. 最新Unity DOTS Physics物理引擎碰撞事件处理

    2024-02-21 21:02:03       65 阅读
  2. DOTS Unity.Physics物理引擎碰撞事件处理

    2024-02-21 21:02:03       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 21:02:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 21:02:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 21:02:03       87 阅读
  4. Python语言-面向对象

    2024-02-21 21:02:03       96 阅读

热门阅读

  1. [精通linux]-302- linux 高级命令

    2024-02-21 21:02:03       47 阅读
  2. 大珩助手相较于其他办公软件插件的独特之处

    2024-02-21 21:02:03       65 阅读
  3. C语言:查找回文数

    2024-02-21 21:02:03       60 阅读
  4. C语言----结构体

    2024-02-21 21:02:03       54 阅读
  5. 将phantomjs制成docker镜像

    2024-02-21 21:02:03       55 阅读
  6. C语言读取文件夹一级子目录的指定类型文件

    2024-02-21 21:02:03       62 阅读
  7. 漫谈C与C++(《Effictive C++》/关于函数重载)

    2024-02-21 21:02:03       48 阅读
  8. C Primer Plus(第六版)16.18 编程练习 第5题

    2024-02-21 21:02:03       49 阅读
  9. C语言:密码强度

    2024-02-21 21:02:03       46 阅读
  10. C语言----数组

    2024-02-21 21:02:03       46 阅读
  11. Node响应Vue axios请求方法说明

    2024-02-21 21:02:03       43 阅读