- Home /
Shotgun Spread Issue
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);
}
Answer by HarshadK · Oct 13, 2014 at 01:59 PM
Your bullets needs to have a LookAt target that has the spread value added to it in order to change their direction.
Check the code below:
function Update()
{
var hit : RaycastHit;
var forward = transform.TransformDirection(Vector3.forward);
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);
forward.x += Random.Range(-spreadRate, spreadRate);
forward.y += Random.Range(-spreadRate, spreadRate);
forward.z += Random.Range(-spreadRate, spreadRate);
bulletInstance.LookAt(hit.point + forward);
bulletInstance2.LookAt(hit.point + forward);
playerSettings.shotgunBullets -= 1;
w();
//shotgun.animation.Play("Shoot");
audio.PlayOneShot(soundEffect);
}
}
Note: It's assumed that you move bullets based on their LookAt.
They still move as one, not spreading around like a real shotgun bullet/shell.
How are you moving your bullets? Can you share that code snippet?
Thank you for your quick response(x2) and I posted my script in the main topic/question.
What's the value of your spreadRate variable. If it's too small then you might not get a spread.
Your answer
Follow this Question
Related Questions
Animation not being played! 0 Answers
Make tranform follow raycast 1 Answer
AI Shooting through walls. 1 Answer
Functions being ignored 2 Answers