Top Down Line Trace

Hello.

After I implemented animations to my character, the attacks broke, they dont go do the cursor because the animation moves the weapon.

I think I can fix it by implementing a line trace beginning from the AttackOri (a scene component directly at the tip of the staff) and ending at the mouse cursor, but I cant figure out how to do it. Is actually driving me insane.

The video bellow shows my best try at it.

World Direction x (Distance from camera to surface ( plus some offset if you want))
Add that to World Location and use it as End.

I understand the solution, but I just got back to developing. How do I get the camera distance from the surface?

I tried like this, but got the same result of the video.

Your Line Trace in the video seems to work properly since the debug line is starting at the tip of the staff and the end at the click location , to me the issue come from the way you define the rotation of the spawned projectile (but it look correct) or maybe you have some outdated code in your projectile

In the video it looks like it goes to the cursor but it does not. When I move the character, is possible to see that it goes to the camera

Ok I see may bad, the issue is that Convert Mouse location to world give you the location of the mouse next to the camera so you need to do 2 traces here, one to get the location the first object under the mouse cursor and the second one from the character to the impact point.

You may want to do some refining of the result of the first trace for example to offset it from the ground, check if the result is in valid (if the player is clicking somewhere where you dont want him to attack you could decide to block it) …


Edit : You can check https://youtu.be/b1_efR9hrT4 for detailed information about the node (his video are old but I started with that long ago and I can tell you they are greats

You can get the distance from Spring Arm → Get Target Arm Length + Some Offset to adjust so that it goes through the floor a bit.

That worked, but maybe my line of thinking is wrong. Since its a top down game, the projectile needs to be always a certain distance from the surface.

The line trace now works, but its acting in a 3d enviroment. I need it to go in the direction of the cursor, but in 2d if that makes sence, so it does’nt go up.

I replied to Extrone bellow, the solution worked but not as I intended

I managed to correct the “going up” issue I think, by spliting the pins and making the start Z axis equal to the end Z axis, but now its a little off

Hey,

I think a simple way of achieving what you want is to just find the intersection between the ray from the mouse cursor and the virtual plane at your projectile origin location Z (height).

You don’t need to trace for collision at all.

Here is a beautiful drawing:

The green horizontal bar is the plane. If we use the Line Plane Intersection blueprint node, we can find the red line vs green intersection. You could use the result as is and then do a linetrace from the green cross to this intersection point. However, I’m assuming that your projectiles have a max distance, or no max distance at all.

So what you want to actually do is normalize this result (the segment from the green cross to the intersection point), so you can then multiply it by whatever max distance you want to use.

This is how it looks in blueprint:

If you do actually want the exact intersection point, you can discard the last few nodes:

Hopefully this is clear.

Please note that in the above blueprint graph, I’m not checking whether there is an actual intersection with the plane (boolean red ‘Return Value’ output). Depending on what kinds of camera angles are possible in your game, it might be safer to check there is an actual intersection before performing the line trace.

2 Likes

First of all, thank you so much for explaining like its for a toddler!!

I did it like this, but didnt work. The line trace appears to me going to the point 0 of the level, if that makes sence

Hey,

I didn’t mention it in the text but if you look closely at the blueprint graph screenshots, the Plane Normal parameter of the Line Plane Intersection node needs to be set to (0, 0, 1). This is because we’re in a top down game and we can assume the trace will always come from the top. This means we want the plane to intersect with to face upwards (+ Z).

■■■■, that actually woked!!

Just some questions, about this node

What exactly it does? When I should think about using it? About the value, how do I figure?

Good question. We’re just multiplying the direction vector by an arbitrarily large value (the direction output of the previous node is a normalized vector, with a length of 1, which isn’t enough to intersect with the plane). The only thing that matters is that this value is big enough to ensure that we do have an intersection with the plane. I put 10000 in this example but you actually need to make sure it is at least the distance from your camera to the projectile origin location.

If you are using a spring arm component for example, it could be the spring arm length x 1.5 (multiplying by 1.5 just to make sure we reach the plane to intersect). Else, you could use the real length of the distance between the camera component and the projectile origin location (again, multiplying by a value > 1 is always a good idea to ensure an intersection).

The line plane intersection node does a simple mathematical check, it doesn’t perform any expensive collision and nothing else of the sort, so having a bigger value than necessary for this multiplier isn’t going to impact performances at all.

Edit: actually, since your game is not a ‘complete’ top down (the camera is at an angle), the above isn’t exactly true. Because the player can potentially click at a ‘grazing’ angle, we need this line distance to be quite a bit bigger than the distance to camera. You should do some tests, in case you notice some intersections are not happening. But I would be you, I would just use a very large value.

Ok, got it.

I used the spring arm before, doing some calculations for the line trace, but it didnt work.

Thanks again for the help, its been 2 days trying on this.