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 :smiley: )

(ps. again: I also use C#, but I can convert from javascript)

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.