Hi,
I am coding (and learning UE4) a game in C++ using Paper2D and I am having trouble getting collision to work.
In my simple scenario I have a flipbook component (enemy) and I want it to fire my delegate if another sprite component (laser) overlaps it.
Here is a snippet of my code:
Enemy constructor:
[FONT=courier new]Enemy = CreateDefaultSubobject<UPaperFlipbookComponent>(TEXT(“Enemy”));
Sprite = Cast<UPaperFlipbook>(StaticLoadObject(UPaperFlipbook::StaticClass(), NULL, TEXT(“PaperFlipbook’/Game/Flipbooks/Enemy_Flipbook.Enemy_Flipbook’”)));
Enemy->SetFlipbook(Sprite);
Enemy->SetPlayRate(2.0f);
Enemy->SetLooping(true);
Enemy->SetRelativeRotation(FRotator(180.0f, -180.0f, 90.0f));
Enemy->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
Enemy BeginPlay():
[FONT=courier new]Enemy->SetCollisionProfileName(TEXT(“OverlapAll”));
Enemy->SetGenerateOverlapEvents(true);
Enemy->OnComponentBeginOverlap.AddDynamic(this, &AEnemy::OnBeginOverlap);
Enemy callback:
[FONT=courier new]void AEnemy::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Red, FString::Printf(TEXT(“Something overlapped with me the enemy!”)));
}
Laser constructor:
[FONT=courier new]Laser = CreateDefaultSubobject<UPaperSpriteComponent>(TEXT(“Laser”));
Sprite = Cast<UPaperSprite>(StaticLoadObject(UPaperSprite::StaticClass(), NULL, TEXT(“PaperSprite’/Game/Sprites/Laser_Sprite.Laser_Sprite’”)));
Laser->SetSprite(Sprite);
Laser->SetRelativeRotation(FRotator(180.0f, -180.0f, 90.0f));
Laser->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
Laser BeginPlay():
[FONT=courier new]Laser->SetCollisionProfileName(TEXT(“OverlapAll”));
Laser->SetGenerateOverlapEvents(true);
That’s it basically. When I run the game, the laser is on the same Z-position as the enemy and when it moves over the enemy, nothing happens.
I have also tried [FONT=courier new]SetCollisionEnabled() in various ways on the enemy component, but it makes no difference.
Am I missing some setting somewhere? This is my first attempt at collision so there could be something I have overlooked.
The enemy sprites are Shrink Wrapped and the laser sprite is set to Tight Bounding Box and all are set to OverlapAll in the editor.
NOTE: I also have OnComponentHit set up but it does not fire either.
Thanks,
M.