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"));
}
question from:
https://stackoverflow.com/questions/65850378/how-do-i-check-for-overlap-with-spawned-actors-ue4-c 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…