Download

Can I return an ECollisionChannel somehow?

Basically what I’m trying to achieve is a universal “Use” function for the player.
I want to press E to pick up physics objects or activate buttons and doors depending on what is in front of the player.
I created a “Use” Actor component C++ class that successfully traces a line from the player and reports a hit on actors.

I created a custom collision channel and called it “Door”;

I added my new collision channel to the trace parameters like so (telling the trace to only detect physics objects and Doors):
FCollisionObjectQueryParams(ECC_TO_BITFIELD(ECC_PhysicsBody) | ECC_TO_BITFIELD(ECC_GameTraceChannel1)),

Now thats nice and dandy but I’d really like to be able to detect which collision channel the trace encountered.
If i could return the Hit collision channel, then in my Use function I could Flow control what to do next (Pick up object, open door, push button, talk…)

**If there is a better way to achieve this please let me know! :slight_smile:

Thanks!!!**

It seems Iv’e solved my own issue. Turns out you can access the collision channel by searching through the Line trace hit result COMPONENT not actor.
Previously, I did something like: HitResult.GetActor()-> NOTHIN USEFUL HERE.

The answer:
If anyone ever has the same question as me, use: **HitResult.GetComponent()->GetCollisionObjectType() **this will return an actual ECollisionChannel(). YAY! Im still open to any suggestions on this practice. is it ok or am i crazy?

I believe the GetComponent method returns the primitive component used in collision detection so it makes sense that the collision channel enum value is stored here.

You may already know this, but it’s useful for others to know - if you change the view in the viewport from (usually) lit to collision you can actually see the primitives used when line tracing/colliding. What you see here is the PrimitveComponent you are getting from HitResult.GetComponent()!

I am new to Unreal by the way, but I have just covered this sort of stuff about a week ago in the course I am following. If some terminology is wrong on my part I apologise and if someone could correct me that would be much appreciated.

In terms of best practice though, this is how they did it in a tutorial by a well respected tutor on Udemy so I imagine so! :slight_smile:

Regards,
Nick

Haha! I took part of that course too. I was looking through it to try to solve this. It is indeed a great way to start coding in unreal.