Hi,
I’m trying to make a predictable faked-random gun recoil pattern for a multiplayer game.
After playing with unity a bit I have a nice result, when unloading my weapon the pattern is always the same and impact decals are placed on points that look like random.
I have still a strange problem : The pattern is different depending if i’m shooting in horizontal or vertical axis.
Here is a screenshot to show you, I have 2 perpendicular walls (perfectly aligned on world rotation) and shot twice on each of them.
I can’t explain this difference.
My prefabs are set like this :
Player > PlayerWeapon > BulletSpawner.
When I shoot, a raycast go forward from bullet spawner.
Here is my shooting code :
rayCastDirection = bulletSpawner.forward;
//if(shootNb > 1) {
// Here is the pattern function.
spread = Mathf.Lerp(0, (Mathf.Cos(shootNb) * 0.05f), (shootNb / BulletsNb)) * 2;
spread = Mathf.Clamp(spread, -gunAccuracy, gunAccuracy);
rayCastDirection = rayCastDirection + new Vector3(
((shootNb % 2 != 0) ? spread : -spread) + (Mathf.Cos(shootNb) * 0.1f),
((shootNb % 2 != 0) ? -spread : spread) + (Mathf.Cos(-shootNb) * 0.1f),
spread
);
//}
ray = new Ray(bulletSpawner.position, rayCastDirection);
if(Physics.Raycast(ray, out hit, FireRange)) {
// hit something
Debug.DrawRay(bulletSpawner.position, rayCastDirection * Vector3.Distance(hit.point, bulletSpawner.position), Color.green, 10, false);
} else {
// nothing hit
Debug.DrawRay(bulletSpawner.position, rayCastDirection * FireRange, Color.green, 10, false);
}
GameObject impactDecal = (GameObject) Instantiate(
wallImpactDecals[Random.Range(0, wallImpactDecals.Length)],
hit.point + hit.normal * 0.01f,
Quaternion.FromToRotation(Vector3.up, hit.normal)
);
Maybe you’ll find something stupid.
Thanks if you can help or give advice.