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.

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.

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.

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:

Your cpp:

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

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!

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. :slight_smile: