Set up Collision Detection Programatically


I’d like to set up static mesh collision detection between actors using only C++ code.

Background: I have an actor with a static mesh that is spawned in to a level in which it then moves about. I’d like that actor to be able to respond to collision events when its static mesh overlaps another actors static mesh.

Problem: I’m struggling to find any helpful information on how I can set up collision detection using C++. Can anyone provide me with any information or guides that can help me do this?

Again, I’d like to avoid depending on the level editor or blueprints if at all possible.

Thanks in advance.


Thanks Kyp for pointing me in the right direction, which is all in all what I was asking for :slight_smile:

So the point to this is that certain components have a FComponenetBeginOverlapSignature called OnComponentBeginOvelap. You add delegate UFUNCTIONS to OnComponentBeginOverlap to respond to overlap events.

You can do this with collision spheres as suggested by Kyp. In my case I used a StaticMesh. Sample code below. If this feels confusing I suggest starting with Master Kyps tutorial.


TSubobjectPtr<UStaticMeshComponent> mStaticMeshComponent;
UStaticMesh* mStaticMesh;

    virtual void OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

.cpp constructor

//Set up static mesh

//Using starter kit sphere for mesh but you can use whatever.
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshOb_AW2(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));

	mStaticMesh = StaticMeshOb_AW2.Object;
	mStaticMeshComponent = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("BasicStaticMesh"));


	mStaticMeshComponent->AttachParent = RootComponent;
	mStaticMeshComponent->AttachSocketName = FName(TEXT("BasicStaticMeshSocket"));

//Collision Setup
	TScriptDelegate<FWeakObjectPtr> onHitFunc;
	onHitFunc.BindUFunction(this, "OnHit");


	mStaticMeshComponent->bGenerateOverlapEvents = true;

Now when I overlap another component with collision enabled, my onHit() method is called.

Final note: If you have several objects / obstacles that you are drag dropping to to your level via the editor, it is important to ensure that they handle overlap events too. You can find out more here: - but if your using this code as a starting point you need to set the objects to respond to overlap events rather than block :slight_smile:

You may want to give this a whirl.

Good luck! :slight_smile:


دمت گرم دادا ، عند مرامی