I am developing a FPS project with Unity, I tried searching for a way to make the gun shoot inside the crosshair but I didn’t find anything about it.
The issue when trying to generate a random Vector to add on the Raycast is that it mismatches the size of the crosshair.
In particular when standing still the bullet goes outside the crosshair:
When moving the bullet matches the crosshair:
When running the bullet doesn’t match the crosshair:
Here is the code for the hipfire spread calculation:
//I did set hipfireSpreadMultiplier to 0.05
float hipfireSpread = 1 / GetGunHipfireAccuracy() * hipfireSpreadMultiplier;
randomDirection = Vector2.right * hipfireSpread;
Transform virtualCamera = PlayerController.SINGLETON.playerVirtualCamera;
if (Physics.Raycast(virtualCamera.position, virtualCamera.forward + randomDirection.x * virtualCamera.
right + randomDirection.y * virtualCamera.up, out RaycastHit raycastHit, 900, enemyLayerMask))
Debug.Log("I did hit " + raycastHit.transform.name);
And here is the code for the crosshair size calculation:
//I did set crosshairSizeMultiplier to 100
float size = 1 / PlayerController.SINGLETON.GetGunManager().GetEquippedGunHipfireAccuracy();
crosshair.sizeDelta = Vector2.one * size * crosshairSizeMultiplier;
I think that the crosshairSizeMultiplier
and hipfireSpreadMultiplier
variables could be the cause of the desynchronization since I did put values I think that are ok.
I have really no clue how to solve this issue since there is no information on the internet.
I did try to change the values of crosshairSizeMultiplier
and hipfireSpreadMultiplier
in order to try finding the right combination so that it would match but I didn’t succeed.
I did try to map the crosshair size to an animation curve, it did kinda work but it isn’t versatile at all and really time consuming plus not precise.