GetOverlappingActors only works at startup

I have a collision box (UBoxComponent) in front (named “CollisionScan”) of the car in the Vehicle C++ template to detect objects infront of the car on a collision course. In the Tick() function I’m calling the GetOverlappingActors() function on every frame to check if there are any objects there, here’s the code:

CollisionScan->GetOverlappingActors(CollisionObjects); 

CollisionObjects is an array, defined in the header file, like this:

TArray<AActor*> CollisionObjects;

But there is no detection, unless I place the object inside the collision box in the editor and then when I start the game the collision is detected.

Why is this happening?

Hi Kristoffer,

To start off with, the reason you’re experiencing this issue is actually an issue related to the collision box itself which seems to only be present in 4.8 but doesn’t occur in our latest internal build, so this will be fixed in the future. The problem itself is that the box collision, even when set to overlap, is causing blocking collision. This explains why it’ll overlap things placed inside of it, but never overlap things that it moves into, as it is simply pushing them out of the way. To verify that your code is working in the meantime, I would suggest trying it in 4.7 as this didn’t occur there.

As for your code itself, I would suggest not calling GetOverlappingActors on tick. The other way to do this would be to use OnBeginOverlap to set a boolean on this class or the overlapping object itself which is used to determine if a certain function that does what you wish to do should be called on tick. You can then also add OnEndOverlap which can be used to set that same boolean to false.

Hope this helps!

Thank you for the quick reply! I will try this tomorrow when I’m back at the office. However, how do you select what version of Unreal Visual Studio should compile to, since I’m running this from there mostly?

Regarding your other remark about OnBeginOverlap. What I want to do is to find the object at the closest distance to the car and report that distance. How would you suggest I do that then? I would need to look up every object within the CollisionScan volume every frame and compare the distance between them. I was thinking just get all the objects every frame, measure the distance to the first one and then store it in a variable, then take the next one and compare that value to the stored value, if it is a shorter distance keep this new distance instead, and continue doing this for all the objects for every frame. Or perhaps one could make it do it only every 3rd or 4th frame to increase performance. If I use Begin and End Overlap wouldn’t that mean that I would have to keep all the objects in memory until they stop overlapping, but still would have to re-measure the distance every frame (or 3rd or 4th) and also check to see if there are any new objects entering the volume every time. Is there really any performance gain for doing so in this case? Oops, that was a long comment :slight_smile:

Unreal is not able to convert my 4.8 project back to 4.7.6, how can I work around that, or when will there be an update for 4.8 that fixes this issue?

I understand that you’re attempting to get the distance between the car and the object, but what is your end goal for this distance value? There may be a completely easier way to do this, but I’d need to know what exactly you’re trying to do.

As far as the fix, it will be included in a new update but I am unsure as to if it will be the next update or not. As far as converting to 4.7.6, it would most likely be easier to create a new project based off the vehicle template and copy the code over and migrate any custom assets instead of trying to convert.

I recreated the project in Unreal 4.7.6 but I get the same problem there as well, the collision is only detected when the object starts out inside the box.

The strange thing is that I did do the Code Tutorial where the character picks up batteries and that code worked, and it is pretty much the same code I’m using, so why isn’t it working this time around?

Are you sure that the box collision that you are using is set to OverlapAll and that the objects you’re attempting to overlap with are Generating Overlap Events? Also, to get a better grasp of what it looks like on your end, can you post a screenshot of your vehicle with the box in the front? I’m afraid there may be some miscommunication. I’ve attached a couple pictures that illustrates how I’m doing/seeing this. I’m printing overlapping actors on tick just to test it.

Sorry for the delayed respons, something else came up. But yes, you are spot on, that is exactly how I’m doing it (but I have a bigger box, but I don’t think that matters). I will fix some screenshots for you and post here.

Okay, here are some screenshots, lets hope it will makes things clearer :slight_smile:
This is the setup of the car and the “CollisionScan” collision box:

And here is the hierarchy outline:

47792-carsetuphierarchy.png

Here is the collision settings for the “CollisionScan”:

47793-collisionscansettings.png

And here is the settings for the orange box to collide with, let’s call 'em “traffic cones” :slight_smile:

47794-traficconesettings.png

Cheers! :slight_smile:

By the way, could you give me the code for that print-on-screen-thing, so that I can past that in my Tick-function as well, might be a better way of debugging.

Oh, never mind, it was easier then I though :slight_smile:
But the messages are only printed when I start the game, but if I drive away from the traffic cone and then come back to it, the messages are not being printed.

By the way, here is the code snippet in the Tick-function of the car, that looks for the collision objects, inspired by the Code Tutorial from Epic with the character picking up batteries.

CollisionScan->GetOverlappingActors(CollisionObjects);
if (CollisionObjects.Num() > 0)
{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::, TEXT("Collision detected!"));
		CalculateClosestDistance(CollisionObjects);
}

I’m still not understanding why we’re getting different results and I’m continuing to look into that. In the meantime I want to confirm, if you drive into these ‘traffic cones’ with the collision box you currently have set up in 4.8, do they get pushed out of the way or do they simply pass through as expected?

That would be helpful as we are getting completely different results.

They pass through the “CollisionScan” box as expected. And they collide with the car mesh and get pushed away, as expected. Perhaps tomorrow I can attache the .h and .cpp file for the car Actor here, so that you can try out my code for yourself?

I’ll be back at the office in about 9 hours, I post them then. Also, it might interest you to know that I created a new project and recreated the scene with only the code needed for this, to make sure nothing else was messing things up. But it gave the same result, and I’ve tried it in both 4.7.6 and 4.8.0 - no luck! However I did update to 4.8.1 today, so perhaps it will yield better results tomorrow, one can always hope! :stuck_out_tongue:

All right, here are the files for the Car Pawn files. The file streams are being used just to try to log information to a text file, I was testing it to see if it could work as a simple solution to just get the information over to another software.link text

I can confirm that I’m definitely getting the same issue, but at the same time I’m completely stumped as to why it is doing this. I wanted give you that update at least but I’ll need time to look into why this is actually happening.

Well at least it’s not just me getting strange result :stuck_out_tongue:
I hope you can figure it out! In the mean time, could you send me that code you used in the working example above and I can test if that works for me?