- Home /
how to make a bullet spread ( shotgun ) in unity 2D
okay so I am working on a 2D shooting game and I made a script which makes the gun aimed at the direction of the mouse cursor, and I have succeeded into making a pistol and an assault rifle but I don't really know how to make a shotgun and I don't know how to make the bullets spread randomly in the direction of the mouse cursor | here is the code of the basic pistol
public class Pistol : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public GameObject reloadingText;
public int maxAmmo = 10;
private int currentAmmo;
public float reloadTime = 1f;
private bool isReloading = false;
void Start()
{
currentAmmo = maxAmmo;
}
void Update()
{
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
IEnumerator Reload()
{
isReloading = true;
reloadingText.SetActive(true);
yield return new WaitForSeconds(reloadTime);
reloadingText.SetActive(false);
currentAmmo = maxAmmo;
isReloading = false;
}
void Shoot()
{
currentAmmo--;
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
}
and here is the code of the bullet
public class Bullet : MonoBehaviour
{
public float speed = 20f;
public Rigidbody2D rb;
public GameObject bulletCollisionEffect;
public int damage = 40;
// Start is called before the first frame update
void Start()
{
rb.velocity = transform.right * speed;
}
void OnTriggerEnter2D (Collider2D hitInfo)
{
Enemy enemy = hitInfo.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
Destroy(gameObject);
Instantiate(bulletCollisionEffect, transform.position, Quaternion.identity);
}
}
help would be really appreciated.
edit: I forgot to put the code where I make the gun aim at the mouse cursor :
public class Pivot : MonoBehaviour
{
public GameObject myPlayer;
// Update is called once per frame
private void FixedUpdate()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
} else if (myPlayer.transform.eulerAngles.y == 180) {
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
}
Answer by Reid_Taylor · Sep 27, 2020 at 07:21 PM
Well looking over your code I don't see anywhere where the bullets look toward the mouse but im guessing its something like this:
public void Shoot ( )
{
firePoint.transform.LookAt(Input.mousePosition);
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
For a shot gun I would do something like this:
public void Shoot ( )
{
firePoint.transform.LookAt(Input.mousePosition);
int bulletCount = 10;
float spread = 1;
Quaternion newRot = firePoint.rotation;
for (int i = 0; i < bulletCount; i++)
{
float addedOffset = (i - (bulletCount / 2) * spread;
// Then add "addedOffset" to whatever rotation axis the player must rotate on
newRot = Quaternion.Euler(firePoint.transform.localEulerAngles.x,
firePoint.transform.localEulerAngles.y,
firePoint.transform.localEulerAngles.z + addedOffset);
Instantiate(bulletPrefab, firePoint.position, newRot);
}
}
Note: this isn't tested and I don't have a lot of experience in 2d lemme know if somethings wrong :)
oh sorry, here is the code where I make the gun aim at the mouse cursor
public class Pivot : $$anonymous$$onoBehaviour
{
public GameObject myPlayer;
// Update is called once per frame
private void FixedUpdate()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = $$anonymous$$athf.Atan2(difference.y, difference.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
} else if (myPlayer.transform.eulerAngles.y == 180) {
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
}
and yeah there is a problem, I added the code, and when I try to shoot, the bullet doesn't go forward and it just activates the particle system at the fire point @Reid_Taylor
Like @vishwah13 said try using eulerAngles instead of localEulerAngels
Answer by vishwah13 · Sep 28, 2020 at 03:18 PM
@Reid_Taylor
i found it it works use firePoint.trasform.eulerAngles instead of localEulerAngles
My code looks like this
if (timeBetweenShots <= 0)
{
quaternion newRot = gunPoint.rotation;
for (int i = 0; i < bulletCount; i++)
{
float addedOffset = (i - (bulletCount / 2) * spread);
newRot = Quaternion.Euler(gunPoint.eulerAngles.x,gunPoint.eulerAngles.y,gunPoint.eulerAngles.z + addedOffset);
Instantiate(projectile,gunPoint.position,newRot);
}
timeBetweenShots = startTimeBetweenShots;
}
else
{
timeBetweenShots -= Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
overhead game in 2d world? 0 Answers
Accurate Text 0 Answers
Unity 2D - Use Digital Painting as Terrain 1 Answer
i have a problem with reloading with Time.time 2d game c# Unity 2020.2 1 Answer