10 Collision and Delegate
084 Collision Presets
碰撞已启用的选项:
- 无碰撞:该组件会穿过其他物体
- 纯查询:用于射线检测、扫描等等,没有物理模拟
- 纯物理:有物理模拟,无查询
- 已启用碰撞:有物理,有查询
角色胶囊体与物体的碰撞属于查询
碰撞响应可以根据物体类别进行调整
085 Overlap Events
碰撞响应中勾选 overlap 会开启重叠检测
on component begin overlap 节点可以单独给某个组件设置重叠事件
086 Delegates
Delegate 是一种特殊的类,可以存储观察者列表,称为委托列表
重叠事件就是用的 delegate,当事件发生时,会广播,绑定的观察者的回调函数被调用
USphereComponent 继承自 UPrimitiveComponent,而 UPrimitiveComponent 中有很多 delegate,其中就包括 on component begin overlap
087 On Component Begin Overlap
| Item.h |
|---|
| class SLASH_API AItem : public AActor
{
protected:
UFUNCTION()
void OnSphereOverlap(
UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& SweepResult
);
private:
UPROPERTY(VisibleAnywhere)
TObjectPtr<USphereComponent> Sphere;
};
|
| SlashAnimInstance.cpp |
|---|
| AItem::AItem()
{
Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere"));
Sphere->SetupAttachment(GetRootComponent());
}
void AItem::BeginPlay()
{
Super::BeginPlay();
Sphere->OnComponentBeginOverlap.AddDynamic(this, &AItem::OnSphereOverlap);
}
void AItem::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
const FString OtherActorName = OtherActor->GetName();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(1, 30.f, FColor::Red, OtherActorName);
}
}
|
088 On Component End Overlap
| Item.h |
|---|
| class SLASH_API AItem : public AActor
{
protected:
UFUNCTION()
void OnSphereEndOverlap(
UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex
);
};
|
| SlashAnimInstance.cpp |
|---|
| void AItem::BeginPlay()
{
Super::BeginPlay();
Sphere->OnComponentBeginOverlap.AddDynamic(this, &AItem::OnSphereOverlap);
Sphere->OnComponentEndOverlap.AddDynamic(this, &AItem::OnSphereEndOverlap);
}
void AItem::OnSphereEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
const FString OtherActorName = FString("Ending Overlap with: ") + OtherActor->GetName();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(1, 30.f, FColor::Blue, OtherActorName);
}
}
|