首先說明一下,官方文檔是錯的,在4.10版本下,綁定函數在角色類的構造函數中不起作用。2016.2.12
這里角色類為例
首先在頭文件中添加:
UFUNCTION() void OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
OnOverlapBegin為自己定義的碰撞函數
然后在Cpp文件中的SetupPlayerInputComponent函數中添加綁定函數:
StartCollectionSphere->OnComponentBeginOverlap.AddDynamic(this, &AThirdPersonCharacter::OnOverlapBegin);
StartCollectionSphere為自己定義的USphereComponent。AThirdPersonCharacter為角色類。
之后添加碰撞函數的定義
void AThirdPersonCharacter::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { GEngine->AddOnScreenDebugMessage(0, 1.0f, FColor::Red, TEXT("!!!!!!")); }