x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

OnComponentBeginOverlap firing multiple times.

I am using the punching montage from the tutorial to use in a short game. I attached sphere components to the hand of enemy characters and they start the montage when they are near my player character. I than used the OnComponetBeginOverlap.AddDynamic on both the sphere on each hand. the function that is called by it collects all the overlapped actors, and if one of the actor is player it reduces player health. the problem is that instead of triggering once each pump, it is triggering multiple times and the player dies in 1-2 punches. how i solve the problem. can someone please help fast.

This is the oncomponentbeginoverlap

void AAI_Character::SetIsPunching() { IsPunching = false; LeftCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision); RightCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision); //Get overlapping actors and store it in an array TArray CollectedActors; CollectionSphere->GetOverlappingActors(CollectedActors);

     //for each collected actors
     for (int32 iCollected = 0; iCollected < CollectedActors.Num(); iCollected++)
     {
         //cast the actor to player character
         AMyProjectCharacter * const MyCharacter = Cast<AMyProjectCharacter>(CollectedActors[iCollected]);
         if (MyCharacter)
         {
             IsPunching = true;
             LeftCollision->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
             RightCollision->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
             LeftCollision->OnComponentBeginOverlap.AddDynamic(this, &AAI_Character::DamageLeft);
             RightCollision->OnComponentBeginOverlap.AddDynamic(this, &AAI_Character::DamageRight);
         }
     }
 }

these are the functions called

 void AAI_Character::DamageLeft(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
 {
     UE_LOG(LogClass, Log, TEXT("Left punch"));
 
     TArray<AActor *> actors;
     LeftCollision->GetOverlappingActors(actors);
     for (int32 iCollected = 0; iCollected < actors.Num(); iCollected++)
     {
         AMyProjectCharacter * MyCharacter = Cast<AMyProjectCharacter>(actors[iCollected]);
         if (MyCharacter)
         {
             MyCharacter->UpdateHealth(-1.f);
         }
     }
 }
 void AAI_Character::DamageRight(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
 {
     UE_LOG(LogClass, Log, TEXT("Right Punch"));
     TArray<AActor *> actors;
     RightCollision->GetOverlappingActors(actors);
     for (int32 iCollected = 0; iCollected < actors.Num(); iCollected++)
     {
         AMyProjectCharacter * MyCharacter = Cast<AMyProjectCharacter>(actors[iCollected]);
         if (MyCharacter)
         {
             MyCharacter->UpdateHealth(-1.f);
         }
     }
 }
 


Product Version: UE 4.19
Tags:
more ▼

asked Jul 05 '18 at 01:50 AM in C++ Programming

avatar image

Gameatro
1 1 2

avatar image HarryHighDef Jul 05 '18 at 08:41 AM

If you are calling SetIsPunching() multiple times it may be binding the event multiple times to the function.

avatar image Gameatro Jul 05 '18 at 09:08 AM

i am calling that function in tick, so i think that must be the problem. i thought it keeps a track of that, like if the collision stated once, then in wont work again till collision ends. i will try calling it outside and see if it works.

avatar image Gameatro Jul 05 '18 at 11:14 AM

I moved the code for begin overlap to the constructor function. but now i am having problem that it is not firing at all. the function is not called. why is this happening? I saw on internet, everywhere it is told to put that function in the constructor.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question