How to detect when a character has collided with a wall

I am trying to code an enemy’s movement, where the enemy would detect the player in front of them, and if the player was there for more than 6 seconds they would charge forward in a straight line to where the player is (or was if the player moves out of the way quickly enough). However, I am having trouble detecting when the enemy has hit a wall sprite or the player. Is there a way to detect when they’ve hit a wall?

I have tried using Component Overlap to do this, however, it needs to be casted to something, and I am unsure on how I would cast a sprite to this. I have also tried to use the Component Hit command but I have no idea how to use it to be brutally honest. I have also tried using a for while loop, but this just causes an infinite loop, which leads to the game crashing.

More Info

My enemy is a floating shadow fist.

I am using the Flying character movement, as this fist will spawn after my character has pressed a button in the level, once the fist has spawned it will float up and down in the centre of the room aiming towards the player, the player has to aim the fist to a weak area of the wall which once the fist hits will be destroyed, but without being hit by the fist itself.
This is the code I am using (Without any loops or variable to tell if it has hit a wall) I wanted my fist to move back first then move forwards continuously until it hits either the player or a physical barrier (e.g. a wall), however, my walls are sprites, not entities so I am unsure of how to cast them in a component overlap command and don’t know how to use the component hit command.

Hello Nebula,

You mention your walls being sprites. Are these sprites part of a tile map? If so, have you setup collision on to the tile map?

If the sprites are not part of a tile map, have you setup collision for them in the editor?

You’re going to want to make sure that you have collision setup for both your fist sprite, and your wall sprite. Then in the Collision’s Category tab, you can setup their respective response channels.

For more information about how the engine handles collision, I would recommend reading the following documentation on [Collision Filtering][3].

Thanks,

Nicholas

Hello Nicholas,

My wall sprites aren’t apart of a tile map and I have checked their collisions, which seem to be in order, also I have set up their collision responses accordingly to what I want to happen.

274609-largebricksprite.jpg

I can set my breakable wall/bricks as a blueprint actor so that won’t be too much of a problem.

My main problem is coding my Shadow Fist enemy to move forward continuously until it hits either the wall or the player, I have tried doing this before with a while loop (which leads to an infinite loop/crash) and a for loop (which with an index too high causes an infinite loop/crash, and would cause my enemy to return to the wrong area).

274631-loopcrash.jpg

274632-hitcomponent.jpg

Is there a way for me to cast my wall sprite to the Hit component (Other Actor), and is there a way for me to send my enemy forward with a loop without my game crashing?

Cordially,

Nebula

I also forgot to mention that this is an old version of my code, as my new code no longer has these loops.
I also plan to change the counting distance variable (which would’ve been used to help my enemy return to its starting point) to the SetActorLocation with the sweep setting on, that way I can easily just save the coordinates of my fist at the start of its attack, and have it reset to it at the end.

I was able to fix my problem myself.

I was able to bypass the infinite loop crashes by using custom events, and I have made a box trigger that I can easily place over my walls, so when my Shadow Fist overlaps it, it will set the Boolean variable ‘Hit Something?’ to true. This also happens when overlapping my character.

Now I just need to set up my Shadow Fist’s Attack Reset and it will be complete.