# Bullet spread changes depending on the camera direction

I'm working on an FPS game. I want to make a little bit of difference in shooting direction, only on the horizontal axis.

To do this, I wrote this code:

Temp_Shake_Horizontal.x = Random.Range(-0.1f, 0.1f);

if (Physics.Raycast(the_camera.position, the_camera.forward + Temp_Shake_Horizontal, out BulletDecal_Hit, Mathf.Infinity))
{
Quaternion HitRot = Quaternion.FromToRotation(Vector3.forward, BulletDecal_Hit.normal);
decals_Objs[decal_Index].transform.position = BulletDecal_Hit.point;
decals_Objs[decal_Index].transform.rotation = HitRot;
}


But I don't know why, when I rotate the camera, the result is changing!

Here is what it looks like when I face in one direction (this is the intended result):

But when I rotate the camera about 90 degrees to the left or right, the result is no horizontal variation: the bullets all line up in a vertical column.

Why does this happen, and how can I solve this issue? I want the result to always be like the first picture, with random variation side-to-side.

Temp_Shake_Horizontal.x = Random.Range(-0.1f, 0.1f);


This shake is always applied along the world x-axis. That means, if you're aiming along the world x-axis, the shake is being applied forward & backward along your direction of fire (ie. not changing the direction).

To apply the shake relative to your current facing direction, do this:

Temp_Shake_Horizontal += Random.Range(-0.1f, 0.1f) * the_camera.right;


You'll want to clear this shake out between uses so it doesn't accumulate - I assume you're already doing that based on the Temp_ name on the variable, but I can't see that part of your code, so you may need to make additional tweaks.

Actually my problem solved by this way :

Temp_Shake_Horizontal = Random.Range(-0.1f, 0.1f) * the_camera.right;


without +

• Ah, since you were setting only the x component, I'd assumed you might already have initialized it with some y value further up that you wanted to retain - that's why I suggested adding rather than overwriting. – DMGregory Sep 29 '18 at 15:36