Update: I finally solved the issue, note that I didn’t work at this 100%, that’s why I found a solution after so much time.
I am facing this issue from a few days, I want to shoot a Raycast inside the UI reticle but I can’t get it working as it should, changing the screen aspect ratio or the reticle size is enough to make the bullet go outside the reticle, for example setting the resolution to 4K is going to make the bullet hit almost the center of the reticle.
Here is the code responsible for handling the raycast:
//If you scroll below this html page, you can see the implementation of the UIHandler methods with comments
Vector3 origin = UIHandler.SINGLETON.gunCrosshair.GetCrosshairPosition();
float pixelDelta = UIHandler.SINGLETON.gunCrosshair.GetCrosshairPixelDelta();
float spread = pixelDelta / Screen.width * Camera.main.fieldOfView;
/* hipfireSpreadMultiplier is a value needed because the spread itself is a very small value.
I am adding to the origin just the right direction for testing */
origin += spread* Vector3.right * hipfireSpreadMultiplier;
Ray ray = Camera.main.ScreenPointToRay(origin);
if (Physics.Raycast(ray, out RaycastHit raycastHit, 900, enemyLayerMask))
Debug.Log("I did hit " + raycastHit.transform.name);
And here is the code responsible for handling the UI reticle:
public void UpdateCrosshairSize(float size)
{
//This method makes the crosshair larger when moving
crosshair.sizeDelta = Vector2.one * size * crosshairSizeMultiplier;
}
public float GetCrosshairInnerEdge()
{
//right is a RectTransform, it is the right white line of the crosshair
return right.position.x - right.sizeDelta.x / 2;
}
public Vector3 GetCrosshairPosition()
{
//crosshair is a RectTransform, parent of all the lines of the reticle
return crosshair.position;
}
public float GetCrosshairPixelDelta()
{
//This calculates the difference between the position of the crosshair and the start of the right reticle line
return GetCrosshairInnerEdge() - GetCrosshairPosition().x;
}
Even if the code seems right the bullet doesn’t match the crosshair.
Below you can see the images showing the issue, the size is less than 20kb each so it’s mobile data friendly. All the screenshots are made by keeping the camera straight forward.
Note that the gun moves up due to recoil but only the crosshair delta is used in the spread calculation.
In particular when firing the first shot the bullet matches the crosshair:
When firing the second shot the bullet touches the right line of the crosshair:
When firing the third shot the bullet goes outside the crosshair:
I have tried to:
- In the spread calculation:
- Multiply by the
CrosshairSizeinstead of thehipfireSpreadMultiplier - Remove the
Screen.widthdivider
- Multiply by the
- Raycast using the original spread instead of calculating the reticle one
- Use a solution suggested by “spacemann” (a stackoverflow user) but it doesn’t work as expected, the raycast goes straight up once every three times:
Quaterion lookDir = Quaternion.Euler(virtualCamera.forward);
Quaternion randomDir = Quaterinion.Euler(Random.insideUnitSphere);
Vector3 shotDir = Quaternion.RotateTowards(lookDir, randomDir, spreadAngle).eulerAngles;




All of your screenshots are showing different backgrounds, gun angles, gun positions, which means your testing isn't consistent. To test this properly, you need to place a plane, in front of,and facing, the camera at the near clipping plane.
– ArachnidAnimalAlright, I will try to do that.
– PatrickmolI did take new screenshots with the camera straight.
– PatrickmolWhat is the hipfireSpreadMultiplier set to? I would think the hipfireSpreadMultiplier should ONLY be used for setting the size of the crosshair. This then affects the pixelDelta of the crosshair. Then you don't need to multiply by hipfireSpreadMultiplier when calculating the new origin position. It seems you might be exponentially increasing the distance of the bullet from the center of the screen by multiplying by hipfireSpreadMultiplier.
– ArachnidAnimalIf I don't multiply by hipfireSpreadMultiplier, the value of the delta is extremely small once divided by the screen width, I have tried to multiply by the crosshair size or doing the pow of the crosshair delta but it was exponentially increasing.
– Patrickmol