Im making a game where waves of enemies spawn and you have to shoot them to win and i want to make a shotgun but i dont know how. How i would like it is you buy it in a menu (i could do that myself) and then when its equipped it sprays 6-10 bullets in front of you. Heres my scripts for shooting.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
private Camera mainCam;
private Vector3 mousePos;
public GameObject bullet;
public Transform gun;
public bool canFire;
private float timer;
public float timeBetweenFiring;
// Start is called before the first frame update
void Start()
{
mainCam = GameObject.Find("Main Camera").GetComponent<Camera>();
}
// Update is called once per frame
void Update()
{
mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);
Vector3 rotation = mousePos - transform.position;
float rotZ = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, rotZ);
if (!canFire)
{
timer += Time.deltaTime;
if (timer > timeBetweenFiring )
{
canFire = true;
timer = 0;
}
}
if (Input.GetMouseButton(0) && canFire)
{
canFire = false;
Instantiate(bullet, gun.position, Quaternion.identity);
}
}
}
thats 1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
private Vector3 mousePos;
private Camera mainCam;
private Rigidbody2D rb;
public float force;
// Start is called before the first frame update
void Start()
{
mainCam = GameObject.Find("Main Camera").GetComponent<Camera>();
rb = GetComponent<Rigidbody2D>();
mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);
Vector3 direction = mousePos - transform.position;
Vector3 rotation = transform.position - mousePos;
rb.velocity = new Vector2(direction.x, direction.y).normalized * force;
float rot = Mathf.Atan2(rotation.x, rotation.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, rot);
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Out"))
{
Destroy(gameObject);
}
}
}
thats 2. yeah pls help