I’m working on an Isometric shooter game, and trying to create bullet spread. I’m using a socket (located at the tip of the gun) as the source of the ‘bullets.’ Now, with the code below, I get the expected spread behavior when facing directly north or south (this is the Z axis), but the more I turn and face the left and right (x axis), the more the horizontal (y axis) spread gets flattened out, to the point where directly facing either direction in the X axis removes all spread from that axis. I have included two images depicting the spread using Debug.DrawRay.
The Goal: Remove this ‘flattening’ of horizontal bullet spread so that it behaves as expected regardless of direciton.
Images of current behavior:
Facing Z axis. Expected spread behavior
Facing X axis. NOT expected behavior
// creates temp variables for shot variance and placement
float deviationX = maxSpread / 1000 * (Random.Range (-shotSpread, shotSpread));
float deviationY = maxSpread / 1000 * (Random.Range (-shotSpread, shotSpread));
//Debug.Log(deviationX + ", " + deviationY);
// Creates shot placement direction as a Vector 3
shotPlacement = muzzleSocket.transform.forward;
// alters based on deviation
shotPlacement.x += deviationX;
shotPlacement.y += deviationY;
RaycastHit hit;
Ray ray = new Ray (muzzleSocket.transform.position, shotPlacement);
// Debug
Debug.DrawRay (muzzleSocket.transform.position, shotPlacement*1000, Color.cyan, 2);
Debug.Log (shotPlacement);
//Checks to see if ray hits
if (Physics.Raycast (ray, out hit)){
// checks to see if collider hit is not null
if (hit.collider != null){
// assigns hitObject
hitObject = hit.collider.transform.gameObject;
// Temporary debugging
Debug.Log ("Gun fired. Hit " + hitObject.name);
} // end of if hit collider isn't null
} // end of physics raycast