I’m currently making a top down shooter and I’m having trouble with trying to figure out how to get bullets to have a “shotgun spread” effect. To explain in more detail, lets take a look at the code.
function FireBullet()
{
//Instantiate bullets
var bullet1 = Instantiate(bullet, spawnBulletPosition[0].position, Quaternion.LookRotation(lookDir)) as GameObject;
var bullet2 = Instantiate(bullet, spawnBulletPosition[1].position, Quaternion.LookRotation(lookDir)) as GameObject;
var bullet3 = Instantiate(bullet, spawnBulletPosition[2].position, Quaternion.LookRotation(lookDir)) as GameObject;
//Set tmpFireTime to 0
tmpFireTime = 0;
//If the sound is playing
if (!audio.isPlaying)
{
//Play laser sound
audio.clip = audioLaser;
audio.Play();
}
}
With the above code, the 3 bullets are firing in a perfect straight line. My question is, how do i get those bullets to slightly curve in or out or just take slightly different paths? It doesn’t necessarily have to be random, but it can be. I was trying to mess with Quaternion.LookRotation(lookDir))
but I have no idea what to do with it or what else I could use.
Nobody?? I’ve been stuck on this for hours upon hours…It’s the only thing holding me back right now. =/
here you go, from my own script. It’s for a FPS but I’m sure you can alter it.
BTW it can work for more than a shotgun, can also be general recoil.
C#
void ShootReg (){
Transform trf = transform; // a little optimization
RaycastHit hit;
Quaternion hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Transform cam = Camera.main.transform;
Ray ray = new Ray(cam.position, cam.forward);
audio.Play();
ray.direction.x += Random.Range(-CurrentSpreadF, CurrentSpreadF);
ray.direction.y += Random.Range(-CurrentSpreadF, CurrentSpreadF);
ray.direction.z += Random.Range(-CurrentSpreadF, CurrentSpreadF);
if(Physics.Raycast (ray, out hit, Distance)){
JS
function ShootReg(){
var trf = transform; // a little optimization
var hit : RaycastHit;
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
ray.direction.x += Random.Range(-CurrentSpreadF, CurrentSpreadF);
ray.direction.y += Random.Range(-CurrentSpreadF, CurrentSpreadF);
ray.direction.z += Random.Range(-CurrentSpreadF, CurrentSpreadF);
if(Physics.Raycast (ray, hit, RayCastDist)){