Question by
HolBol · Nov 30, 2020 at 02:27 AM ·
raycastaxisraycasthitspread
Bullet Spread axis compression
Hi there! I'm trying to implement some random bullet spread (for a shotgun effect). I've noticed both my implementations have the same unexpected issue. When firing along the world z axis, the spread appears as expected, as below:
However, when firing along the x axis, the x axis spread of the shotgun appears to compress towards 0, like below.
This is the method I'm currently using, where I'm implementing the spread value per "pellet" cast.
void FireScatter(){
//one round should give multi pellets
roundsInMagazine--;
nextFire = Time.time + rateOfFire;
float rad = 0.0f;
float spreadX = 0.0f;
float spreadY = 0.0f;
//fire a ray for every pellet, with each one getting random spread
for (int i = 0; i<numShotPellets; i++) {
rad = Random.Range(0.0f, 360.0f) * Mathf.Rad2Deg;
spreadX = Random.Range(0.0f, spreadFactorX / 2.0f)* Mathf.Cos(rad);
spreadY = Random.Range(0.0f, spreadFactorY / 2.0f)* Mathf.Sin(rad);
Vector3 deviation = new Vector3(spreadX, spreadY, 0.0f);
Vector3 direction = cam.transform.forward;
Ray ray = new Ray(cam.transform.position, direction + deviation );
RaycastHit hit;
if (Physics.Raycast(ray, out hit, range, mask)){
if (hitEffect)
{
Instantiate(hitEffect, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
else
{
Debug.Log("No hit effect assigned");
}
}
}
}
Could someone point out to me what the error is in the logic here that causes this issue? Just having a little trouble with it myself. Thank you in advance!
Comment