but the problem is that if I change the X or Y axis instead of Z, to rotate a vector of a hit vertically, it goes up if I’m watching a side of the map, and it goes down if I’m watching the other side.
What can I do to rotate the vector of a hit vertically?
So basically we attempted to set up our shotgun bullet spread using vectors with coordinates this way (see attached image).
Our issue is that we can’t seem to get our vectors to rotate along the x axis. Only the Z axis seems to work, therefore our bullet spread system is only half done.
Does someone know why this is happening and/or what could possibly be causing this?
Better start working with angles and rotators. Shotgun spread is more easily represented in terms of angles, so you just create a rotator with the yaw and pitch of whatever spread you want. FRotator::RotateVector will transform a vector, so you can just take your camera forward vector and transform it using each shotgun spread rotator.
Also, you’d probably want to randomize shotgun spread, rather than use predefined coordinates. You can use FMath::VRandCone for that. Let’s say you want your maximum shotgun spread to be 2 degrees:
See if that helps. By the way, this is just stylistic advice but you have much to gain and nothing to lose by separating this kind of stuff over multiple lines of code. Rather than cram everything on a single line, it’s a lot more readable to calculate each vector separately before passing it to your trace. Readability helps avoiding precisely this sort of issue.
And you mention Quake as a reference, I’m pretty certain Quake 3 randomizes its pellet spread.
The issue seems to be that the vectors are erroneously influenced by the direction you are aiming at. We need to find a way to fix this issue which could be ovverriding the rotation set for the vectors.
This is actually unrelated to your code and your problem, which I can’t help to fix, but this is relevant to the topic, which is to rotate a vector. Future readers who are looking to rotate a vector, this is how to do it:
//Try to get a rotator from anywhere that you want your vector to face towards.
//In this case, I used my UStaticMeshComponent rotation of my AActor.
FRotator Rotation = this->MyMesh->GetComponentRotation();
//This is an arbitrary target vector you want to rotate. It can be any vector you designate.
FVector Vector = FVector(1.0f, 0.0f, 0.0f);
//To rotate, I call on FRotationMatrix class, pass in the rotator, and call on RotateVector()
//to rotate a vector for me. Pass it in like this:
Vector = Rotation.RotateVector(Vector);