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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How can I apply damage using Collision Overlap?

Hey! I am trying to create a simple damage script using collision overlap. What I am trying to do exactly is that I call a function on "OtherActor"s Script called "ApplyDamage" and send the damage that the projectile should do to the player. Anyone know how I would do this?

Here is what I am using at the moment.

 void ACollectable::OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
 {
     if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
     {
         if (OtherActor->IsA(AFPSCharacter::StaticClass())) //check if a player was hit
         {
             GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, "Collision with health object!");
             OtherComp->ApplyDamage(Damage); //obviously this does not work, but maybe something along those lines?
             MeshComp->DestroyComponent();
         }
     }
 }

Thank you in advance!

Product Version: Not Selected
Tags:
more ▼

asked May 10 '14 at 01:28 PM in C++ Programming

avatar image

Borzi
174 43 39 48

avatar image Borzi May 12 '14 at 08:23 AM

Wow, did not expect this to be so difficult (pretty easy to do in Unity). Nobody have any ideas? Should I try something else?

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

2 answers: sort voted first

Okay so the method I used to make this work was casting:

 void ACollectable::OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
 {
     if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
     {
         if (OtherActor->IsA(AFPSCharacter::StaticClass()))
         {
             GiveDamage(Cast<AFPSCharacter>(OtherActor)); //cast to "OtherActors" FPSCharacter Class
             GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, "Collision with the collectable!");
             MeshComp->DestroyComponent();
         }
     }
 }
 
 void ACollectable::GiveDamage(class AFPSCharacter* pl)
 {
 if (pl)
     {
     pl->Health -= Damage;
     }
 }
more ▼

answered May 12 '14 at 05:10 PM

avatar image

Borzi
174 43 39 48

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

You could call AActor::TakeDamage and override the function in your character class.

more ▼

answered May 12 '14 at 04:19 PM

avatar image

cbuttner
19 2 4 10

avatar image Borzi May 12 '14 at 05:12 PM

I ended up doing something different, but how does "TakeDamage" actually work? Do you have to define your health variable in it or something? Always confused me a lot....anyway, thanks for your reply! +1 This is probably a pretty good solution to the problem as well

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
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