Hello, Im trying to make a shotgun script but im failing at making the bullets spread. The bullets instantiate fine but they go on the same direction, anyone has any idea how to fix this ?
Here is my code :
function Update()
{
var hit : RaycastHit;
var forward = transform.TransformDirection(Vector3.forward);
forward.x += Random.Range(-spreadRate, spreadRate);
forward.y += Random.Range(-spreadRate, spreadRate);
forward.z += Random.Range(-spreadRate, spreadRate);
if(Physics.Raycast(transform.position, forward, hit))
{
if(Input.GetButton("Fire1") && canShoot==true && playerSettings.shotgunBullets > 0 && shotgun.gameObject.active == true)
{
var bulletInstance : Transform;
var bulletInstance2 : Transform;
bulletInstance = Instantiate(bullet, barrelEnd.position, transform.rotation);
bulletInstance2 = Instantiate(bullet, barrelEnd.position, transform.rotation);
bulletInstance.LookAt(hit.point);
bulletInstance2.LookAt(hit.point);
playerSettings.shotgunBullets -= 1;
w();
//shotgun.animation.Play("Shoot");
audio.PlayOneShot(soundEffect);
}
}
The bullet HAS to be a prefab!
Thank you for your time!
Edit : The bullet script :
#pragma strict
var bulletSpeed : float = 10;
function Awake()
{
Destroy (this.gameObject, 5);
}
function Update()
{
transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);
}
function OnCollisionEnter()
{
Destroy (this.gameObject);
}