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"

Character Collision

So I've searched and searched... maybe I'm just passing the answer over and over again. I'm just trying to catch when the character hits a wall, that's it.

I've done this in blueprints, but trying to get it over to c++ is just not working, so I was hoping someone could explain how I can get the hit result when running into a wall in c++.

I have the source, but surely this can be done in a c++ project without having to go into the actor class? I feel dumb asking such a simple question, but at this point I'm just wasting my time by not asking.

Capsule, Character, whatever, just want to catch collision. I just want to be able to run into a wall and output a debug message saying its hitting the wall.

Product Version: UE 4.14
Tags:
more ▼

asked Nov 27 '16 at 05:28 PM in C++ Programming

avatar image

Eilios
44 7 9 9

avatar image Eilios Nov 27 '16 at 10:40 PM

Just to note for anyone else wondering the same dumb things I do, the answer to the question seems to be no, you cannot do this in the character.cpp class. You have to extend off the Actor class, or something to that effect with the correct lingo included. :)

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

1 answer: sort voted first

There are two ways of detecting collision for an actor. Either you use the built in Hit function or you bind a function to a UPrimitiveComponent's OnComponentHit multicast delegate.

Approach #1: Override

 virtual void NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit);

and implement your logic there. Don't forget to call Super::NotifyHit!

Approach #2:

Say you have a reference to a specific primitive component that supports collision like a static mesh or a capsule collider. You can add a custom UFUNCTION to its OnComponentHit delegate like this:

 UFUNCTION()
 void YourCustomOnHitFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
     
     
         
 void AYourActor::BeginPlay()
 {
     Super::BeginPlay();
     YourPrimitiveComponent->OnComponentHit.AddDynamic(this, &AYourActor::YourCustomOnHitFunction);
         }
     
 void YourCustomOnHitFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
 {
       //Your logic here
 }

Don't forget to remove your function from the delegate for example in EndPlay by calling RemoveDynamic instead of AddDynamic. It uses the same signature.

Sorry for the weird layout but the code sample block is not that useful in terms of easy to read layout.

more ▼

answered Nov 27 '16 at 09:10 PM

avatar image

Schadek
198 6 4 12

avatar image Eilios Nov 27 '16 at 09:48 PM

How would I put this in the character class though? It only throws errors, as it should because it's actor.

Using the first option (in the .h): Error C:\Users****\Documents\Unreal Projects\SoD3D\Source\SoD3D\SoD3DCharacter.cpp(337) : error C3867: 'AActor::NotifyHit': non-standard syntax; use '&' to create a pointer to member

Then putting this in the character.cpp (throwing the error, line 337 is the super:: part):

 void AActor::NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit)
 {
     Super::NotifyHit;
     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Collide"));
 }

Says for the Super::NotifyHit that "Class UObject has no member "NotifyHit" with intellesense.

Was just hoping I didn't have to go out of the character class to get the character collision.

Option 2, same issue really, it's all wanting character because I'm trying to do this in the character class, it seems...

I'm assuming I'll have to make a class to extend off AActor or do some coding in that class instead. Please correct me if I'm wrong.

avatar image Schadek Nov 27 '16 at 10:36 PM

Of course you need to implement it in your own class. I suggest looking at classes like APawn to understand how inheritance works in Unreal C++.

Your header file: http://pastebin.com/JXr0k58e

Your cpp: http://pastebin.com/WgkSST6v

This is just an example actor that has a capsule component and gets notified once this capsule collides with anything.

avatar image Eilios Nov 27 '16 at 10:39 PM

Got ya, I was hoping there was a way to just do it in character, but that's just my misunderstanding of the classes and UE4 I guess. Thanks!

(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