extend distance of my line trace after adjusting the inputs with random vectors

I have a third person camera line trace to the centre of my screen for a fixed crosshair (called Dynamic Crosshair in my build because it changes size).

I have a second line trace running from the gun to the centre of the screen for a non-static crosshair that will show the player that their bullet will be blocked by an object in front of them like so (a crosshair will go where the green hit box is)

My code for these looks like this:

My third line trace is for my actual bullet trajectory. and is set up in the the picture below.

Now, here lies my problem:

The bullet line trace input for the end point takes the hit point from the non-static crosshair, then adds a clamped random range based on the character’s speed and stance to adjust accuracy.

Everything works exactly as planned except one thing. The random range affects all three axis, meaning sometimes the line trace falls short of the intended hit marker. (it moves the bullet slightly left, right, up, down, but also forward - not an issue - and back. This is in the picture below

I need to increase the distance of the line trace without affecting any other direction, and I can’t for the life of me work out how.

The start point is the bullet. The end point is the crosshair hitbox + random number multiplier and the random sometimes makes the numbers too short a distance.

I’m assuming you have
FVector StartPoint, EndPoint;

EndPoint is CrossHairHitBox + RandomOffset

FVector direction = EndPoint - StartPoint;
direction = direction.Normalize();

double ScaleFactor = 100; // 100 cm extension. This should be the maximum offset possible.
EndPoint += direction * ScaleFactor;

The Blueprint is the same operations in order… like so:

edit: To calculate the maximum offset (to use for the scale factor), it’s square root of (3 * MaximumOffsetPerAxis^2). Or just do twice MaximumOffsetPerAxis.

If they are all different, it’s sqrt(x^2+y^2+z^2) where x,y,z are the maximum offsets in each axis. Add 1% extra or something to avoid rounding errors being short.

edit2: This should also work. I added the scale factor using the random offset length.

edit3: There was a node that multiplied by 0.01. It was supposed to be 1.01. The screenshot above has been fixed.

1 Like

You can add horizontal and vertical deviations on top of your intended location like this, without moving the end point forward or backward:

I’m not sure if I understood the objective correctly though.


Omg because this was the first thing that came to my mind, I was expecting something like it therefore couldn’t really tell what I was looking at in your blueprint code @AlienRenders so didn’t really thought much about it. But today while I was doing something completely irrelevant, this answer of mine randomly came to my mind and I said to myself, hold up… this isn’t right! After thinking for a bit I realized my new answer could be what you already posted, got back to this topic to check and found out that it is! Funnily enough, I already had something similar in use in my own project :rofl:

^Yep, the right answer is the post above^

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.