Line trace from actor location to mouse cursor and infinity on side scroller project

I want to trace a line from my actor location to mouse cursor on a side scroller project.
My issue is that when I multiply my World Direction vector with a big number to trace to infinity, the line trace doesn’t mantain the direction to my mouse cursor.

Code:

Here is a sample video of what’s happening:

Yes, my idea is to do exactly that, I want to extend my traceline until it hits the first object, but how can I achieve that if I don’t extend my line to “infinity”.

I think you’re bound to run into problem if you try and end the line trace outside your world. Why not trace as far as you need?

It only needs to extend to ‘all reasonable objects in the vicinity’. A trace of about 2000 should do it I guess. I’m thinking that when you see the ‘jump’ in mapping, it just has nothing to hit. Maybe take that into account in your programming.

Sorry but that doesn’t help. It doesn’t matter how much I extend the trace line, it doesn’t cross my mouse cursor if I extend that line in that direction.

Hmmm, afraid I don’t know too much about 2d, but I’m assuming then the problem is you’re doing a 3d trace in what is supposed to be a 2d world.

What are you trying to achieve with the trace? Looking for objects?

I’m trying to shoot a bullet from my actor to the mouse cursor position, but the bullet should continue his route, not stoping at the mouse cursor location.

BTW, the side scroller project it’s a 3D world.

So, here’s what’s going on:
When you multiply the World Direction by that insanely large number, it’s setting the end point of the trace far into the distance. And because this is a perspective camera, that point far into the distance no longer lines up with where the cursor is located on the screen. So how we do we solve this? 2 possibilities, depending on what you need specifically for your project:

  1. You don’t multiply the World Direction by an insanely large number. Instead, multiply it by the distance from your camera to your character. This can be calculated manually or you can just use the spring arm length. This will keep the trace on the same plane as your character. Use this if you don’t intend to detect items in the background.

  2. However, if you want to be able to trace to anything onscreen, use two line traces. The first one will use the World Location from the 'Deproject Screen to World" node as the start, and the end point will be (World Location + (World Direction * 10000)) Pretty much what you have now, but without breaking the vector to make a new one. The second line trace will be the same as the first possibility listed above, but will instead use the distance from the hit result of the previous line trace + 1 rather than the spring arm distance. The + 1 will ensure the trace actually hits instead of stopping just before. This will let you trace to anything that’s onscreen.

I hope this solves your issue.

Blueprint setup for Option A: Answerhub Linetrace Option A posted by Cage518 | blueprintUE | PasteBin For Unreal Engine 4
Blueprint setup for Option B: Answerhub Linetrace Option B posted by Cage518 | blueprintUE | PasteBin For Unreal Engine 4

Hey Cage, amazing answer and very detailed, thank you. First option is valid for me, but I have two issues with that one. The traceline stops at mouse cursor, I need that the traceline continue that direction until it hits something. And the other issue is that I need to make the traceline goes only on X=Player_Location_X, if not, this is what happens:

Okay, I see where I went wrong with the first option. Since it’s only getting the distance from the character to the camera, all of the line trace end points are basically on a giant sphere with the camera as it’s origin, which is why they’re skewing away from the character. To fix this, we’re going to change some things.

First, so that we know the line trace will be on the same plane as the character, we’re going to make a new vector that uses the character’s X location as it’s own X. Next, break apart the vector we were using as an endpoint, and plug the Y and Z values into our new vector. If we were to plug this new vector into the end point of our line trace, we’d see that the trace wouldn’t make it to the mouse cursor, but it would still always point at the mouse and always be on the same plane as the character, which is a good start for the second part of the fix: extending the line beyond the cursor.

Now that we know we just need to extend the line trace into the distance, here’s what we’re going to do. We take our new vector, and subtract the player’s location from it. This gives us a directional vector from the player to the cursor. If we normalize this vector so that it only has a length of 1, we can then multiply it by any float to make it whatever length you need. Now we just add the player location to it and plug it in as the end point to our line trace.

And if you need help following all of that, here’s the new blueprint structure: Answerhub Linetrace Revised A posted by Cage518 | blueprintUE | PasteBin For Unreal Engine 4

And there we go, a trace from the character to the mouse that extends into the distance.

Hey Cage, excellent answer again, thank you! We’re almost there, the traceline is quite off from the mouse cursor when I got far away from the player location, everything else works perfect. See pictures please.

296477-1.jpg

296478-2.jpg

Okay okay okay. I did it. I don’t think it should be as complicated as it is, but this works. So first, the explanation for what was going wrong with the first revision:

I took a small shortcut when I broke the vector apart and replaced the X coordinate with actor’s x coordinate. While the trace lined up with the cursor when it was close, that small shortcut grew the further the cursor got. And it comes back to using the spring arm’s length, because that length didn’t extend out far enough as the cursor got further from the center of the screen. And finding that extra length needed is why this was such a problem. So how do we fix it?

Well.

In short, we get the angle between the World Direction the deprojection gives us and the vector from the camera to the spring arm’s origin. Then, using some trig, we get the proper length we need to multiple the World Direction by. However, it’s still slightly imprecise, so we still make a new vector using the character’s X location to keep everything on the same plane. Multiply by a big number and we now have a proper line trace:

Amazing! It works perfect now. I really appreciated all your explanations, very detailed, thank you!

@Cage518 blueprintUE | PasteBin For Unreal Engine 4 this link doesn’t take you to a blueprint anymore, any chance that someone has a pic of the solution?