My hit markers are deflecting from my crosshairs based on how far I turn my character. I am trying to have a sphere trace go from the barrel of my gun to where the crosshairs are pointing (in the center of the screen). My solution was to do a line trace from the center of the camera along the forward vector (through the crosshairs), then break the hit result and find the ‘Impact Point’, and then use that ‘Impact Point’ as my ‘End’ on my sphere trace. This works for the most part, except for one thing.
When I play it, the sphere trace goes to my crosshairs initially, but if I rotate, the sphere trace exaggerates my rotation. As I turn further left, my sphere trace drifts further and further left from the crosshairs. If I turn right, the crosshairs move slowly (but noticeably) further to the right of the crosshairs. I am at a loss for what to do. Please help.
Understood. However, the distance between the hit marker and the crosshairs changes based on how far I turn the player. By changing the FPS template crosshairs’ offset, the distance between crosshair and hit marker wouldn’t change.
Are you using the standard first person template? If so, the crosshair there is not in the center of the screen, it’s offset a bit.
I think your problem is on the END point, when you calculate forward vector x 1000.
Try this, instead of getForwardVector, use this function I created.
Initial location: will be same location as your start.
Initial rotation: pass your Player.GetControlRotation value (NOT THE CAMERA VALUE)
I think this will solve your problem.
I’ll try that on Monday and let you know what happens. Thank you for the help! Vectors are a strange beast.
So, no luck so far. I did some messing around (and I’m not 100% sure of what I did), and the hit markers are no longer drifting from the crosshairs. However, the line traces are now consistent in their offset. I am not great with vector math. It seems UE4 treats location data the same as unit directional data. And why do you suggest adding the initial vector (location) to the forward vector (directional)?
In my blueprint, I am now doing a forward vector trace from the player camera, finding the impact point, then calculating the ‘Look at Rotation’ from the gun barrel to the first impact point, and then converting that to a unit vector for the second line/sphere trace. Any and all help/input would be super welcome.
You have to add your Forward Vector x 1000 to the camera location, like you do for the second trace. And make it more than 1000; 1000 is just about 10 meters, it’s too little, the trace may sometimes end in the air with no hit at all.
Edit: oh, it’s 10000, that should do.
Forward Vector is basically a direction, it has no data about the initial location, so if you want to get the correct hit point, you have to set the origin for it, and that is your camera location.
Edit: yes, location is also a vector in UE4, you should think of it as a vector from 0,0,0 to where the actor is. Directional vectors are always normalized, i.e. their length is always 1 unit.
You were right. I had to add in the three different vectors. I will post a screenshot later, when I get back into the project. Basically what I did was…
- Line Trace and then Break Hit Result
- Find Impact Location
- Find ‘Look at Rotation’ between the barrel of the gun and the Impact Location
- Get ‘Forward Vector’ of that rotation
- Add that Forward Vector * 10,000 + Gun Barrel location + Player Camera location
Everything seems to work fine now. Yay! Thank you for helping and explaining vectors!
I’m glad you achieved what you wanted, but I don’t get something.
4. How can you get a forward vector of the rotation? Rotation itself is already a vector. You can get Forward Vector of an actor. Find Look At Rotation already returns you the direction from your gun barrel to the impact point.
5. If you add three vectors, the result will not be correct. When you get the vector from the Find Look At Rotation node, you only have to add it to the Gun Barrel location, not the Camera location. (Edtd: Unless the Gun is a child to the Camera and you add the Gun’s relative location; in that case the result would be correct; but instead you can get the Gun’s world location and leave the Camera be)
Just a little trick to do to check if it’s right: select everything in the level and move it alond the axes by a few thousand units. I think the default level is somewhere around 0,0,0 in the world coordinates, and maybe if anything with the vectors is not right, you won’t see a great difference. But if your character’s location is somthing like 7648,2345,990, the error of adding more vectors that you should will be quite visible.