I'm currently working on a top-down shooter and I want to make shotgun
I'm currently working on a top-down shooter and I want to make a shotgun but I have no idea how can I make bullet spread these are my scripts, please forgive me if I have some big mistakes:
public Transform firePoint;
public GameObject bulletPrefab;
public int maxAmmo = 10;
public int currentAmmo;
public float reloadTime = 1f;
public bool isReloading = false;
public float bulletForce = 20f;
void Start()
{
currentAmmo = maxAmmo;
}
void Update()
{
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
IEnumerator Reload()
{
isReloading = true;
Debug.Log("Reload.....");
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
}
void Shoot()
{
currentAmmo--;
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}
this is the bullet script:
public Rigidbody2D rb;
public int damage = 50;
public float speed = 10f;
private void Start()
{
rb.velocity = transform.up * speed;
}
private void OnTriggerEnter2D(Collider2D other)
{
Enemy enemy = other.GetComponent<Enemy>();
Destroy(gameObject);
if(enemy != null)
{
enemy.takeDamage(damage);
}
Debug.Log(other.gameObject);
}
}
You could instantiate the bullets in a specified area or in a range randomly
Answer by hesteefen · Jul 24, 2021 at 10:30 AM
I still have a problem @Anonymous620 I'm sorry I found this problem very late I just tried out the script
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
IEnumerator Reload()
{
isReloading = true;
Debug.Log("Reload.....");
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
}
void Shoot()
{
currentAmmo--;
for (int i = 0; i <= numBullets; i++)
{
bulletParent.transform.position = new Vector3(Random.Range(Centre.transform.position.x - range, Centre.transform.position.x + range),
Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range);
Instantiate(bullets, bulletParent.transform.position, bulletParent.transform.rotation);
}
}
there is an error at end the of this line Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range);
Sorry, I've just realised that Center is already defined in unity. Try changing it to _center.
Future viewers, keep in $$anonymous$$d that this is the right answer to the code fix. this is not the actual code/answer to the question. The answer to the official question is below this with the script called BulletSpread. This problem isn't relevent any more as i have fixed the issue in the original code.
Answer by Anonymous620 · Jul 19, 2021 at 11:26 AM
public class BulletSpread : MonoBehaviour
{
public GameObject bullets;
public GameObject bulletParent;
public GameObject Centre;
public float numBullets;
public float range;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
SpreadBullets();
}
}
void SpreadBullets()
{
for (int i = 0; i <= numBullets; i++)
{
bulletParent.transform.position = new Vector3(Random.Range(Centre.transform.position.x - range, Centre.transform.position.x + range), Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range));
Instantiate(bullets, bulletParent.transform.position, bulletParent.transform.rotation);
}
}
}
see if that works
i think the position should not be random.....only the direction
that is not a problem.
thank you for helping me :)
Well no problem, if youl ike it that way, you can easily omit any of the two......also @Anonymous620 it would be better if you could convert your comment to an answer so that hesteefen can accept it
is the bullet parent meant to be a child for the player or is it a enitre different gameobject?
It is a child of the player gameObject, but it can be anything
Answer by hesteefen · Jul 24, 2021 at 11:06 AM
I still have a question
void Shoot ()
{
currentAmmo--;
for (int i = 0; i <= numBullets; i++)
{
bulletParent.transform.position = new Vector3(Random.Range(Centre.transform.position.x - range, Centre.transform.position.x + range), Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range);
Instantiate(bullets, bulletParent.transform.position, bulletParent.transform.rotation);
}
}
I'm not very sure why but there is an error at the end of this line
bulletParent.transform.position = new Vector3(Random.Range(Centre.transform.position.x - range, Centre.transform.position.x + range), Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range);
I'm sorry I asked this problem a week later I was a little bit busy
Vector3
constructor requires 3 floats for xyz and you provided 2.
bulletParent.transform.position = new Vector3(
Random.Range( Centre.transform.position.x - range , Centre.transform.position.x + range ) ,
Random.Range( Centre.transform.position.y - range , Centre.transform.position.y + range ,
0f
);
Technically this is not an error, because Vector3 has 3 constructor variants:
Vector3 a = new Vector3( 1f , 2f , 3f );
Vector3 b = new Vector3( 1f , 2f );// new Vector3( 1f , 2f , 0 ) equivalent
Vector3 c = new Vector3();// new Vector3( 0 , 0 , 0 ) equivalent
Your answer
![](https://koobas.hobune.stream/wayback/20220613040509im_/https://answers.unity.com/themes/thub/images/avi.jpg)