How to get the first Collider on a blueprint?

The problem:

I have a base class that contains a ProjectileMovement component, as well as a bunch of other functions and properties etc. Because of the way Unreal handles collision, the root component of this base class would have to be a collider (not ideal because children may require different collision shapes) or the ProjectileMovement component will need to have its target component explicitly set in each child blueprint (not ideal because the whole point of inheritance is to not repeat yourself).

The way Unreal handles collision means I am left finding another way to make inheritance and collision play nicely together.

An approach that can be used in other engines is to simply find the first component in the hierarchy that implements an interface or base class. But it seems Unreal doesn’t use a base class for colliders, or at least doesn’t expose this to blueprints. Is there a function I’m not aware of that can accomplish this?

Or am I on the wrong track altogether? I feel like inheritance and collision shouldn’t be at odds with each other, and aren’t in Unity and Godot (though I haven’t used Godot in ages and it may have changed), so maybe Unreal development uses a different paradigm or pattern? Surely there is something I missed because I find it hard to believe that AAA games can be made without basic things like this.

Hey @JohnAZoidberg!

So if you use the node “GetComponent” it returns the first component in the hierarchy of the type you specify. There is no “collision” parent (that I know of, there likely is one) but you can specify Sphere, Capsule, and so on.

Also I don’t know who told you a collider has to be the root… That’s 100% not the case. I actually tend to use a scene component (an Empty) as the root for a lot of things and the collision works the exact same. :slight_smile:

So if you use the node “GetComponent” it returns the first component in the hierarchy of the type you specify. There is no “collision” parent (that I know of, there likely is one) but you can specify Sphere, Capsule, and so on.

Yes, but if I have to specify the shape of the collider component then I have to do this in every class that inherits from my base class. Which kind of defeats the purpose of inheritance. Coming from Unity I would just call GetComponent<Collider>(); and the first component that implements Collider will be found. So a parent class doesn’t need to know or care what shape the collider is, only that it exists and can be referenced.

In Unreal doesn’t have a base class for colliders then I’m just going to have to accept that I’ll be repeating myself a lot in child classes.

Also I don’t know who told you a collider has to be the root… That’s 100% not the case. I actually tend to use a scene component (an Empty) as the root for a lot of things and the collision works the exact same. :slight_smile:

I may not have explained myself well, but if the collider is not the root component then it has to be explicitly assigned to whatever movement component (eg projectile movement) you are using in order to actually collide with things.

So I can either have every child class have the same collision shape via the root component of the base class that ProjectileMovement uses automatically, or repeatedly set the collision shape and tell ProjectileMovement to use it in every class that derives from the base class.

My contention being that constantly repeating oneself in a child class is what inheritance was created to fix, and I’d rather assume that I am missing something than assume that Unreal’s blueprints are designed in such a way that this isn’t possible.

Idk if you have any experience with Unity, but a base class might exist as…

[RequireComponent(typeof(Collider))]
public class SomeBase : MonoBehaviour
{
    private Collider collider_component;

    private void Awake(){
        collider_component = GetComponent<Collider>();
    }
}

SomeBase doesn’t care what kind of collider it is. SomeBase just needs to know that a collider exists somewhere on the object that this script is on.

I expected Blueprints to be much less flexible than actual code, but something like the above example seems pretty standard game dev stuff and I’d be very surprised to find that blueprints has no capability like that.

Well the thing is, there’s a few different ways to go about that exact thing. I do, however, agree and understand that it is strange that the three base collider classes don’t share a parent of just… Collider.

But if I wanted to do what you’re trying to do on a parent, I’d have this:

Threw that together in about 1 minute. About thirty seconds of that was organizing the links :laughing:. Which is ABSOLUTELY slower than C# would have been for me in Unity, yeah.

But really I just manually grabbed the GetComponentByClass, InputKey, IsValid, and created the Collider variable, Duplicated twice, set all 3 through the dropdowns, and added a printstring for error testing.

Edit: I reread what you’re looking to do. This on the parent, paired with the above, should fit your needs exactly, if I’m understanding correctly.

That will work, it just feels so… icky. I guess that’s the tradeoff for using visual coding instead of c++. I learned Rust specifically to avoid c++, but it might be time to just bite the bullet and commit to learning c++.

It has to be a mesh, which is the one with the colider, So you can leave the base class mesh empty, and then change that mesh in the children and each child can have a different colider.

why do you have to do that?

you only need one event hit event in the base class

Is it possible to enable collisions with static geometry when the collider is not the root in that case? All projectiles go through static / stationary geometry in this situation.
With a collider at root they stop / bounce as expected.

Enabling sweep on collisions for the projectile movement doesn’t alleviate this behavior and is prevalent even in 4.27

In what strange situation do you need that behavior?

I was doing a quick project where I was showing someone how to emulate a god of war throw / return axe where I wanted to do different actions depending on if the blade hit a wall or the handle (axe was spinning in motion).

Sure I could have just ditched the projectile movement but that was what the op of the thread was asking about.

In the situation there the mesh is the root + 3 box colliders and there is projectile movement nothing registers on default geometry.

Only when I went into the environment and set all of it’s static geometry to register overlaps did it start to handle it (not an ideal situation)

You do lose the collision preset setting on the object though and easier spatial awareness.

I would also have to calculate the impact point and check against a position on the object / maybe even rotation to see which part was hit. (an axe should only stick with the blade and bounce off it any other part would hit)

In theory I could check materials, but I would need extra material passes / draw calls then.

Aa, you want different behaviors depending on which area of the weapon was hits.
Then use a skeleton mesh import with a bone for each different zone.
Now you create a physical asset and for each bone you can create the collision shape you want to add and have a different physical material.
I guess there is some way to know what physical material the weapon hit.
or not :rofl:, I guess the purpose of the projectile component is for simple things.

I’m not entirely sure how Unreal’s collision works. The documentation is sparse and nothing is intuitive coming from other game engines.

For example, I have a simple cube in my level. It has collision set to block all, with collision enabled, and Generate Hit Events on. This cube doesn’t move.

I have an actor with a static mesh (sans collision shape) and a ProjectileMovement on it. This also has a CubeCollider component set to block all, collision enabled, and event generation enabled. The Event Hit this is set to simply print the name of the other object. This projectilemovement sends the projectile toward the cube at <-500,0,0>

The cubes pass through each other and nothing is printed to the screen.

In the projectile, in Event Begin Play, if I set the UpdatedComponent on the ProjectileMovement to the CubeCollider I get a hit event and the projectile stops.

So it seems that in order to get the engine to acknowledge the collision for a collision shape that isn’t the root component it needs to be explicitly applied to the projectile movement.

This is ok until I want to have a base class with a projectile component and a child class with an arbitrary collision shape.

I think this is more of a projectile component problem than a general collision.
The documentation states
"Normally the root component of the owning actor is moved, however… "

Unreal is like Miss Rontemeyer if you don’t do what it should be, things get complicated. :joy:

The person who programmed the projectile component decided that the object in root is the one used for the collision.
And in my opinion I can’t think of any normal reason why the colliding object doesn’t get rooted. :sweat_smile:

I’m aware that’s how projectile movement works. What I’m attempting to accomplish is to set the UpdatedComponent of the ProjectileMovement to a collision object (eg BoxCollider, SphereCollider, etc) in a child class. This means I need some way to getting any one of those classes, which don’t share a parent class that is exposed to blueprints.

The whole thing just makes inheritance with blueprints a giant hassle.

I have a projectile base and all its children have different collision shapes and everything works without a line of code as I already explained…

…But ok, I think the best way to search for components in children, is to search for component by tag and set to UpdatedComponent, this can be done in the parent’s begin play.
Since you’ll have to go through the children anyway adding the colider by hand putting an tag on it isn’t a extra work.