c++ - 处理与 TriggerSphere 和其他 Actor 的重叠?

标签 c++ collision-detection unreal-engine4

我有一个 ATriggerSphere,它设置为跟随角色,旨在响应与其他 Actor 的重叠。我正在尝试这样做:

void AScrollsCharacter::BeginPlay()
{
    // Call the base class  
    Super::BeginPlay();

    //Create activate trigger radius
    activateRadiusTrigger = GetWorld()->SpawnActor<ATriggerSphere>(ATriggerSphere::StaticClass(),GetActorLocation(), GetActorRotation());
    activateRadiusTrigger->SetActorHiddenInGame(false);
    USphereComponent* colSphere = (USphereComponent*) activateRadiusTrigger->GetCollisionComponent();
    colSphere->SetSphereRadius(ACTIVATE_RADIUS);
    colSphere->bGenerateOverlapEvents = true;
    colSphere->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
    colSphere->OnComponentBeginOverlap.AddDynamic(this, &AScrollsCharacter::OnOverlapActivateSphere);

}

void AScrollsCharacter::OnOverlapActivateSphere(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("We got a collision."));
}

void AScrollsCharacter::Tick(float deltaSeconds)
{
    Super::Tick(deltaSeconds);

    //Set the radius trigger to follow the player
    activateRadiusTrigger->SetActorLocation(GetActorLocation());
}

不过,OnOverlapActivateSphere 似乎不会在发生碰撞时被调用。由于角色在这个球体内部,而且我还没有告诉触发器忽略它,我希望消息会在每个滴答声中打印出来。我是误会还是遗漏了什么?谢谢!

最佳答案

回答

不,它不会在每次报价时都被调用。 OnOverlapActivateSphere 只会在开始重叠时被调用一次。因此,为了再次调用它,您必须将对象移出球体,然后再将其移入球体。事实上,您在每次滴答时都将触发器传送到角色位置并不意味着您的玩家可以脱离它。

如果连一次都没有调用

查看 collision overview由 Epic Games(UE4 的创建者)提供。所以:

  1. 确保至少将一个对象设置为重叠另一个对象;
  2. 确保您已为您的球体检查生成重叠事件(但我看到您已在代码中将其设置为 true,所以应该没问题);

建议

看起来您不需要单独的参与者作为触发器。无论如何,它都依附于你的角色。我建议改为将其作为一个组件。您可以从 USphereComponent 继承,实现您的触发逻辑,使该组件成为角色的子对象,并将其附加到构造函数中的根组件(不在 BeginPlay 上)。

关于c++ - 处理与 TriggerSphere 和其他 Actor 的重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39424033/

相关文章:

c++ - 查找整数值并将该值传递给循环中的另一个整数但是整数被重置为初始值

C++结构数组初始化

swift - 父评估失败 : variable not available - sprite kit

c++ - 如何修复虚幻引擎4中的 "Pointer to Incomplete class type is not allowed"错误

c++ - 访问属性还是使用 getter 方法?

javascript - 检测 Object3D 是否正在接收阴影 (r86)

c# - Unity 2D 碰撞检测

c++ - 有没有办法动态更改比较运算符?

c++ - 在运行时创建和更改游戏状态

c++ - 我的 'Person' 类有什么问题?