- Home /
Multi-gun Aiming System
Hi guys,
I'm still moderately new to Unity, and this is the first time that I am going to be working with ships. On these ships, there are various weapons on various parts of the ship. I am using a Raycast from the camera to determine where the guns are to aim. However, the guns don't seem to be aiming at the same spot...
If you would like to see more info (scripts, how things work, etc.) please comment below and I will update this topic.
Please help!
(ps. I am trying to replicate War Thunder's aiming system for ships, with a little bit of inaccuracy in the aiming of each gun, so if you could offer some expertise it would be greatly appreciated :D )
(ps. again: I also use C#, but I can convert from javascript)
I was doing something like this for an FPS where you could potentially have a weapon in either hand, a head-mounted spotlight, and shoulder cannons, and was using Quaternion.LookRotation() to orient the weapons towards my target point each frame. Is that what you're currently using? You can also implement spread by using Random.Rotation and Quaternion.Lerp (Lerp between your ai$$anonymous$$g rotation and your random rotation, with t chosen to deter$$anonymous$$e the final spread, where a value of 1 means a potential spread of 180 degrees (meaning all the way in the opposite direction), 0.5 -> 90, 0.25 -> 45, etc.
Yeah I was initially using LookRotation(), but I switched to using a empty transform in the same position, with LookRotation() at the target. I then rotated my turret to the angle of that empty transform. The inaccuracy problem I fixed, but thanks though!
Answer by Nerull22 · May 27, 2018 at 09:22 PM
So here are my immediate thoughts on the subject. I'm assuming you have a Vector3 as your aiming location. So having your aiming system send that exact aiming location to each individual gun. I would draw an absolute gizmo for aiming. Meaning that it should store the exact point it should be trying to hit. Then a separate color line that draws where it's trying to aim. That way you know that it's aiming at the right point, but when you have the spread applied you can see visually in your scene that it's trying to hit something and the spread is what you want.
Now give each individual gun a maximum spread amount. Treat this value as a radius; so half of what you want the actual spread to be. Then do a random range between 0 and this value. Multiply that by Vector3. One as a constant value can be multiplied by an entire matrix and then add your result to the definite aiming location. That will give you your offset aiming Vector3. I'll write some code below that more details this out as I'm sure it's hard to follow in the text. I'll explain some of the content of the answer below in case people don't understand some concepts.
[SerializeField]
private float _aimingSpread = 1;
public Vector3 GetAimingPosition(Vector3 definiteAimingLocation)
{
float randomSpread = Random.Range(0, _aimingSpread);
Vector3 spreadAimingLocation = Vector3.one * randomSpread;
spreadAimingLocation += definiteAimingLocation;
return spreadAimingLocation;
}
Mind you that this has not been tested, but I'll explain the thought process.
So "definiteAimingLocation" will be the value that you're aiming location will be trying to hit. This is the exact Vector3 that you want each gun to try and hit.
The "_aimingSpread" will be a value in the gun of its potential inaccuracy. This is a radius of spread, so make sure it's half of what you want.
So you'll generate a random spread that will act as your inaccuracy. Then you'll multiply this by Vector3.one which will give you how off the gun will be. Vector3.one * 0.5f will equal (0.5f, 0.5f, 0.5f). Constants times Matrices will apply to every value in the matrix.
Then add that Vector3 to the definite aiming location and it will give you the Vector3 of the aiming spread location. So let's say you're trying to hit (2, 5, 6.5) and your spread generated a Vector3 of (0.5f, 0, -1). Adding these together will give you your final aiming position of (2.5f, 5, 5.5f). Aiming the gun there will give you that spread you're looking for.
I know this is long and probably repetitive, but I hope it helps.
Currently I do have a script on my camera that is using a raycast to reposition a transform (which I have my guns ai$$anonymous$$g at). But as for the dispersion, you do have a pretty interesting concept (which I think with a little modification could be splendid). But using Vector.one, wouldn't that cause the dispersion to be diagonal (eg. ai$$anonymous$$g at 1,1,1 or 5,5,5 or -2,-2,-2)?
Sorry for the late reply. Yes, you're right. That would probably not be what you're looking for. You would need to generate a random Vector3. Sorry, wrote that a little quickly and didn't test it. You may even want to do something like a total inaccuracy. Like all your random can not exceed a maximum innacuracy of 3 units off. So your offset could be (2,0,1) but no more. Can have some interesting rule sets in there. But yes, you'll have to generate a random Vector3. $$anonymous$$y bad. :)
It's all right! I took what you said and got a reliable dispersion system, and have now moved on to other problems. But thanks for your help!