I am quite new to C++ and UE4. I am creating a basic snake game in UE4 and I can't get the snake to trigger an overlap event when it overlaps with the fruit that the snake will eat. The fruit is spawned on BeginPlay at random coordinates each time I start.
I have tested overlapping with actors that are not spawned (i.e., actors that are already in the scene) and this works fine. It seems that there is an issue with testing for an overlap on a spawned actor. I am using the IsOverlappingActor() function.
I am thinking there is something wrong with the Fruit pointer. I am not sure what though because, when I eject during play and click on the spawned fruit, in the details section the Fruit pointer appears to be initialised with the fruit blueprint.
This is my spawn code:
void ASnake::SpawnFruit()
{
if(!FruitClass)
{
UE_LOG(LogTemp, Error, TEXT("FoodClass NOT ASSIGNED"));
return;
}
if(FruitClass)
{
const float RandX = FMath::RandRange(0.0f, 2000.0f);
const float RandY = FMath::RandRange(0.0f, 2000.0f);
const FVector SpawnLocation = FVector(RandX, RandY, 50.0f);
const FRotator SpawnRotation = SnakeHead->GetComponentRotation();
Fruit = GetWorld()->SpawnActor<AFruit>(FruitClass, SpawnLocation, SpawnRotation);
Fruit->SetOwner(this);
}
// TODO: Delete and spawn new food when overlap with snake
// TODO: Spawn snake segments when overlap with food
}
This is the code in the tick:
if(Snake->IsOverlappingActor(Fruit))
{
UE_LOG(LogTemp, Warning, TEXT("OVERLAP OCCURED"));
}
Instead of calling that function on Tick, you should instead create a USphereComponent or UBoxComponent on your Snake to act as a Trigger Volume that you can then subscribe to its OnBeginOverlap and OnEndOverlap delegates to check for overlaps of Fruit Actors.
// .h
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UBoxComponent* Volume;
// .cpp
ASnake::ASnake()
{
Volume = CreateDefaultSubobject<UBoxComponent>(TEXT("Volume"));
Volume->SetupAttachment(GetRootComponent());
Volume->InitBoxExtent(FVector(1000.f, 1000.f, 1000.f));
Volume->SetCollisionResponseToAllChannels(ECR_Overlap);
}
void ASnake::PostInitializeComponents()
{
Super::PostInitializeComponents();
Volume->OnComponentBeginOverlap.AddDynamic(this, &ASnake::OnVolumeBeginOverlap);
Volume->OnComponentEndOverlap.AddDynamic(this, &ASnake::OnVolumeEndOverlap);
}
void ASnake::OnVolumeBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (Cast<AFruit>(OtherActor))
{
// Do something to the Fruit.
}
}
void ASnake::OnVolumeEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (Cast<AFruit>(OtherActor))
{
// Do something to the Fruit.
}
}
Also ensure that your Fruit Blueprint collision component has GenerateOverlaps enabled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With