Managing collision profiles in code?

I’m struggling to find examples of how to manage collision profiles in code. With blueprints, there are checkboxes that let you specify that “walls” block “bullets” but “bullets” ignores other “bullets”, etc… Very convenient.

In code, though, all I’ve found so far is

SetCollisionObjectType(): sets the collision profile for a mesh component
SetCollisionResponseToChannel(): sets a mesh component’s response to a particular channel

But how do I specify, in code, that all “bullets” should ignore other “bullets,” and so on? I guess I could set the response for every single mesh, but that seems really inefficient, both in coding time and execution time.

Thanks.

C++ Code For You

Hi there!

Here’s some code you can use as a reference for customizing the collision channels in code!

I highlighted the most relevant function names.

Keep in mind you have these options

ECR_Block
ECR_Overlap
ECR_Ignore



//~~~~~~~~~~~~~~~~~~
CollisionComp = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereComp"));
	
//Set Root
RootComponent = CollisionComp;
//~~~~~~~~~~~~~~~~~

//Collision Size
CollisionComp->InitSphereRadius(5);

//Query AND PHYSICS
CollisionComp->BodyInstance.**SetCollisionEnabled**(ECollisionEnabled::QueryAndPhysics);

CollisionComp->BodyInstance.**SetObjectType**(ECC_PROJECTILE);

//AI
CollisionComp->bCanEverAffectNavigation = false;


//Block All!
CollisionComp->BodyInstance.**SetResponseToAllChannels**(ECR_Block);

//Ignore Camera
CollisionComp->BodyInstance.**SetResponseToChannel**(ECC_Camera, 	ECR_Ignore);		

//Ignore Character Capsules!
CollisionComp->BodyInstance.SetResponseToChannel(ECC_JOYCAPSULE, 	ECR_Ignore);		

//Ignore Joy Zoom !
CollisionComp->BodyInstance.SetResponseToChannel(ECC_JOYZOOM, 	ECR_Ignore);


:slight_smile:

Rama

Thanks, Rama.

I just thought maybe there was a way to set the response based on channel, as opposed to doing it for every single component. I mean, I might have 50 types of bullets and 50 types of walls. It would be nice if I could just say, globally, that all walls block bullets, etc, rather than having to do it in every constructor for every bullet and wall component.

I know this is a super old post but for anyone that comes looking for this: if you have all your bullets inheriting from a single parent, put this guy in the parent:

Parent Bullet:

CapsuleComponent->SetCollisionResponseToChannel(COLLISION_PROJECTILE, ECR_Ignore);

Then make a function in your child class that returns that capsule component so you can put this in your child bullets if you want specific bullets to block others -

Child bullet:

GetCapsuleComponent()->SetCollisionResponseToChannel(COLLISION_PROJECTILE, ECR_Block);

That way all bullets will inherently ignore other bullets unless this is placed in there ObjectInitializer code