Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

unreal engine4 - How do I check for overlap with spawned actors? UE4 C++

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...