0
\$\begingroup\$

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

Image of bullet holes on wall, showing random spread

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.

Image of bullet holes making a sharp vertical line

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.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$
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.

\$\endgroup\$
0
0
\$\begingroup\$

Actually my problem solved by this way :

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

without +

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Commented Sep 29, 2018 at 15:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .