I am currently trying to get an enemy in a 2D game I’m making to bounce off of walls, similar to how the old DVD screensaver would bounce. However, I’m having trouble getting the program to understand where to correctly bounce, as it will currently only bounce along one axis no matter what I do. What I want to have is a system that detects in what direction a collision occurred, before adjusting the movement vector accordingly. How could I achieve this?
I am very new to Unreal, so any help would be appreciated!
Yeah, it’s not quite right, is it? ( my idea either ).
What’s missing it direction of object before the collision and after. Thinking about it…
Direction is your trace:
And it can be mirrored like so:
When the hit takes place, make a line trace and you get the impact normal:
That does make sense, and I have been using a raytracing method before in order to try and get this to work. However, I feel like there’s something math-y that’s still missing from my current method, as the bounce is present but not acting in the correct direction. I’ve attached the current method I’m using to achieve this (and again I’m fairly new to Unreal, so it might look a little gross).
(Also I just realized I had it set to Normal instead of Impact Normal. I changed that, however the issue persists.)
I tried out this method, even using the debug arrow to see if it’s working. However, when I use this, the entity does not seem to react to walls at all, and no arrow can be seen. Maybe there’s something wrong with the origin of my raytrace? It’s possible that something is going awry when my program attempts to actually find the correct ray.
What I want to have is a system that detects in what direction a collision occurred …
You didn’t say if you’re using a Hit or an Overlap event? Because if you’re using Hit event, then there’s no need for linetraces, since the Hit event already contains the Hit Structure.
Enable Draw Debug on the Trace node - this will help you visualise it. Hard to tell how it should work judging by the attached image. If in doubt, consider attaching a screenshot of the desired result.
Currently it is set to an overlap event, hence why a linetrace would be needed.
This image shows the end result I want to achieve. Currently, the T Cell would be set to move along the first blue line. As soon as it hits the wall, I want its movement vector to reflect over that black line, continuing along the blue. I think that it’s entirely likely that my initial capturing of the T Cell’s movement is incorrect, as currently the end of the ray is set to “closest point on collision,” which may not be correct.
Hello again, I’ve still been working with the same problem. I’ve gotten it so that an arrow is drawn towards the point of impact, giving me the proper ray I’m looking for, and I’ve initialized your line of code. However, upon putting it into practice, the T Cell doesn’t actually move anywhere. It understands the general direction, but it does not know how to apply that to a specific magnitude. How could I make it so that the new direction has a proper magnitude?
How could I make it so that the new
direction has a proper magnitude?
Multiply the direction by some value. 1uu = 1cm. In my example above the arrow’s magnitude is 100uus.
I tried that to no avail; no matter what value I multiply it by it still seems to not move, even on values as high as 100,000. I’ll attach a screenshot of what the code block currently looks like.
The attached image is the current method in which it moves. The function is called on EventTick, with a default Speed of 100. I also have a small function that runs on EventBeginPlay that allows me to set the variable Direction, which is then plugged into the “Random Direction” input.
If there’s anything else you’d like to see of my current method I can provide what is needed. Thank you for being so patient with all of this!
Not important here, but I meant degrees above, not radians. 90 radian cone would be quite a strange looking thing… Meh.
It should be sweeping; everything is setup so that it should sweep and understand that the wall is a wall. Both are also set to BlockAll, so they should interact with one another. The program also is set to print a value whenever a staticmesh is collided with, and while it does print that it met with a staticmesh it does not change course.
As for the space itself, while it’s technically set on a 2D plane, the world is 3D, so hopefully that helps.
Perhaps show us how you actually move it.
similar to how the old DVD screensaver
Following up on this. If I wanted a similar behaviour without simulating physics, I’d:
Not sure, judging by what is shown and with a speed of 100 you should be getting some movement here. The direction is normalised, so multiplying it by 1 million changes nothing.
Consider printing the transform and see if the number add up.
Also, check my answer below, perhaps it will push you (no pun intended) in the right direction.