Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

alt text

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.

alt text

Product Version: UE 4.19
capture.jpg (55.5 kB)
capture3.jpg (80.0 kB)
more ▼

asked Apr 18 '19 at 01:49 PM in Blueprint Scripting

avatar image

6 1 3

avatar image NicholasMont ♦♦ STAFF Apr 18 '19 at 02:13 PM

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?

alt text

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.

alt text

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



avatar image NebulaOrNutella Apr 18 '19 at 08:37 PM

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. alt text

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). alt text alt text alt text

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?



attackcode.jpg (86.8 kB)
loopcrash.jpg (12.2 kB)
hitcomponent.jpg (18.9 kB)
avatar image NebulaOrNutella Apr 18 '19 at 08:46 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I was able to fix my problem myself. alt text 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.

fixed.jpg (111.7 kB)
more ▼

answered Apr 22 '19 at 02:39 PM

avatar image

6 1 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question