Collision Detection in C++

Hey everyone,
So I am working on a melee attack with a sword, which will eventually be able to do things like, chop off zombie limbs or stop a animation if it hits a dense wall or chop through it if it is thin enough, etc… stuff like that, what I have to do first is detect when an object is hitting it, which I am trying to do right now:

This is the code for picking up the item and adding the collision code and later removing the item and collision code:

void APlayerCharacter::Use(AUsableItem* curItem, FName Socket){
	curItem->getItemMesh()->AttachTo(GetMesh(), Socket, EAttachLocation::SnapToTarget, true);
	TScriptDelegate<FWeakObjectPtr> BeginDelegate;
	TScriptDelegate<FWeakObjectPtr> EndDelegate;
	BeginDelegate.BindUFunction(this, FName("OnWeaponOverlapBegin"));
	EndDelegate.BindUFunction(this, FName("OnWeaponOverlapEnd"));
void APlayerCharacter::OnWeaponOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool fromSweep, const FHitResult& result){
	GEngine->AddOnScreenDebugMessage(1, 1, FColor(1, 1, 1, 100), TEXT("HIT"));

void APlayerCharacter::OnWeaponOverlapEnd(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex){
	GEngine->AddOnScreenDebugMessage(1, 1, FColor(1, 1, 1, 100), TEXT("NoHit"));


void APlayerCharacter::RemoveItem(AUsableItem* curItem, FName Socket){
	curItem->ApplyWorldOffset((FirstPersonCamera->GetComponentRotation().Vector() * 200), false);


curItem is the melee Weapon, and it should be obvious that I am adding a delegate to when the actor begins/ends overlapping. The Item does work with physics, such as moving boxes around etc… Also I have set up the UFUNCTION() tag in the header with OnWeaponOverlapBegin/End. I cannot seem to figure out what the problem is, but no matter what, I dont see “Hit” being shown on the screen…


By the way, I am using a USkeletalMesh For the weapon, so cant check just a simple collision Mesh

Still having an issue with this, can anyone help? Seems like this would be a common thing to do in a game

Hello joelComberiati,

It seems like the issue here is that your collision is set to Block All, but you’re attempting to base your collisions off the overlap events. You’ll need either set your weapon to be OverlapAll (Or some form of that) or change your collision events to OnActorReceiveHit or something of the sort. You also may wish to set up a Box Collision (Or any type of collision volume, not sure on your weapon’s shape), attach it to the weapon, and run the overlap/hit events based off that.

Thank you!,
So I changed collision profile with:
but it still does not work, I am wondering if it is because there is only a physics asset attached and not a collision mesh,
is there a way to use the collision volume that is in a physics asset?

Did you set a CollisionObjectType?

This is a good read if you’re unsure if you’ve understood collision correctly:

Thank you, this has actually been very helpful in confirming what I was unsure of. But I have no set the Collision Object Type (by setting the Collision Profile) But I do not know if they physics asset mesh is being used as a collision mesh, and I am not sure how to do that. The reason why I want the physics asset mesh, is because for each object i set whether I can use a single/multi convex hull, or possibly a box or sphere, because each object may be drastically different shape.

I don’t really know what you’re aiming for. A good approach might be to drastically simplify your code, remove possible sources of error and start with the easiest case to see if that works. Then find your way towards your desired procedure step-wise to see where there’s a problem.

That is what I have been doing for the past few weeks…
Basically I want my USkeletalMesh to use the attached physics Asset Collision Volume to detect Collision. This is as far as I have gotten, yet it does not work. It seems right, but I have not used Unreal a whole ton, so I am not sure how this works.

Actually now the collision works somewhat, it is just not at all accurate, sometimes saying it is hitting when it is not, and not hitting when it is…

And I think this issue is due to it being touching the character…

So I figured out why it was acting so strangely.
I used this instead:

	curItem->collMesh->OnComponentBeginOverlap.AddDynamic(curItem, &AUsableItem::OnWeaponOverlapBegin);
	curItem->collMesh->OnComponentEndOverlap.AddDynamic(curItem, &AUsableItem::OnWeaponOverlapEnd);

The MoveIgnoreActors allows the weapon ignore the weapon that it is surrounding and the player, that way there is no chance for collision.

Thanks to and I used both of your answers to find the solution to this problem

Posted 3 days ago, huh? And working on it for the “past few weeks” mirrors exactly what I’ve been facing too. I’m using a skeletal mesh with the physics asset as well, and I now have the physics assets colliding, albeit I disabled animation for the time being, and switched to using a BOX collider. This link will show the use of OnActorHit.AddDynamic(this, &AMyPawn::OnHit) that does work for collisions with everything :slight_smile:

Oddly enough though my bullet isn’t colliding using the same technique.

Well, I have actually been working on this long before I posted it here. I only ask questions after I have spent time working on it for a while. No need to bother others for information that I wont spend the time to find myself. But thanks for the input.

If you have a blueprint for your weapon it’s a lot easier not to set the collision profile stuff up in C++. It’s way easier to just set it up in the gui for the mesh.

Here’s a good video to watch A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

Set up your component in the editor and just override the response functions in code.

Ah, you see I am a programmer, and from what I understand, blueprints do a a lot, but c++ will always be able to more because it allows you more freedom. Anyways, I switch the Profile between block all and overlap all when i pick up/drop the weapon. I have decided to use blueprints sort of like objects that I can drop onto the game.