How do I give my top down game bullet spread?
I'm currently in the process of making my first game, and I'm trying to figure out how to add a small amount of bullet spread to my gun. I tried out a few solutions but they didn't work for me (though that was mostly copying some code and being confused). Would anyone give me a hand?
Here is my code for shooting:
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 20f;
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}
}
And here is my code for looking at the mouse: public class PlayerMovement : MonoBehaviour { public float moveSpeed = 10f;
public Rigidbody2D rb;
public Camera cam;
Vector2 movement;
Vector2 mousePos;
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
If you're wondering, all of this code is from Brackey's tutorials so far.
Do you need the bullets to spread out over time (rotate the bullets) or do you just need the bullets to be positioned in a spread and go in the same direction?
I just need the bullets to be slightly spread out from the start, that way the guns aren't laser accurate.
sorry for all the questions but does the gun shoot multiple bullets at a time or is it just a single bullet
Answer by Anonymous620 · Oct 04, 2021 at 12:11 AM
Here is some code that makes the bullet instantiate in a specified range from the centre point. this also allows you to instantiate multiple bullets at a time like a shotgun.
public GameObject bullets;
public GameObject bulletParent;
public GameObject centrePoint;
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(centrepoint.transform.position.x - range, centrePoint.transform.position.x + range), centrePoint.transform.position.y);
Instantiate(bullets, bulletParent.transform.position, bulletParent.transform.rotation);
}
}
the centrePoint will need to be a child of the player and the bulletParent should be a child of the centrePoint. the numBullets is the number of bullets that you want to have shoot. the range is how far the bullets can instantiate from the centrePoint. let me know if you have any issues with it
@MaskedRecruit Just checking if this worked for you. I didn't write this code for a top down shooter but rather an fps and only edited it for a top down shooter. so i am not sure that it works for a top down shooter. Can you please let me know if this worked for you. thanks
Oh shoot, sorry I forgot that I posted this. I tested out the code, but I don't know if it properly spread them out. I ran into the issue that the bullets all collided with eachother the moment they spawned, so they all just got destroyed instantly
If your code destroys the bullets when oncollisionenter or ontriggerenter is triggered, you will need to add an if statement like this:
if(!collision.gameobject.CompareTag("Bullet Tag"){
Destroy(this.gameobject);
}
this if statement means that the destroy code won't run if 2 bullets are colliding.
I'm testing it again and now I'm having a different issue. The code is just changing where the bullets spawn, not the angle that they're facing. What I'm trying to change is the angle that they travel.