- Home /
Need help with coding Assault Rifle in unity 2D
Im trying to code an assault rifle script and it doesn't work as intended, i know what the problem is but i don't know how to fix it or a workaround, im using two different scripts, 1 for the bullet prefab (im shooting with prefabs) and another for the rifle functionality, i have made a pistol and it works completely fine, the rifle is the one that i need help with
this is the code that im using for the bullet script
Rifle Bullet Script:
using UnityEngine;
public class RifleBullet : MonoBehaviour
{
public float speed = 80f;
public Rigidbody2D rb;
public int damage = 45;
void Start()
{
rb.velocity = transform.right * speed;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.name != "Player" && collision.tag != "Portals" && collision.tag != "Coins" && collision.tag != "Bullet")
{
Destroy(gameObject);
//Debug.Log(collision.name);
}
Enemy enemy = collision.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
Debug.Log(enemy.health);
}
}
private void OnBecameInvisible()
{
Destroy(gameObject);
}
}
this is the one im using for the rifle script
Rifle Script:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Rifle : MonoBehaviour
{
public Transform RiflefirePoint;
public GameObject bulletPrefab;
public bool canShootRifle = false;
public int rifleBulletAmmo = 32;
private int rifleBulletReserve = 128;
private bool canReloadRifle = true;
public Text bulletText;
public bool rifleIsBeingUsed = false;
void Update()
{
Pistol pistol = GetComponent<Pistol>();
if (Input.GetKeyDown(KeyCode.Alpha1))
{
rifleIsBeingUsed = true;
pistol.pistolIsBeingUsed = false;
if (rifleBulletAmmo != 0)
{
canShootRifle = true;
}
Debug.Log(pistol.pistolIsBeingUsed && canShootRifle);
}
if (rifleIsBeingUsed)
{
bulletText.text = rifleBulletAmmo.ToString() + "/" + rifleBulletReserve.ToString();
}
if (rifleIsBeingUsed && !pistol.pistolIsBeingUsed)
{
if (Input.GetButton("Fire1"))
{
if (canShootRifle)
{
shootRifle();
}
}
if (!canShootRifle && Input.GetKeyDown(KeyCode.R) && rifleBulletReserve != 0 && canReloadRifle)
{
canReloadRifle = false;
StartCoroutine(rifleReload());
}
}
if (rifleBulletAmmo == 0)
{
canShootRifle = false;
}
}
void shootRifle()
{
Instantiate(bulletPrefab, RiflefirePoint.position, RiflefirePoint.rotation);
rifleBulletAmmo -= 1;
}
IEnumerator rifleReload()
{
yield return new WaitForSeconds(3);
canShootRifle = true;
rifleBulletAmmo = 32;
rifleBulletReserve -= 32;
canReloadRifle = true;
}
}
so basically the problem is that i have made it so that it has ammo so when it reaches 0 it can't shoot and the pistol works well with this because it isnt automatic so it subtracts 1 from the ammo every time i click the mouse, but when i try the same thing with the rifle since you are able to hold it it substracts 1 from the ammo every frame instead of every bullet shot, so the magazine gets empty in less than a second, i just need a fix for this.
Answer by Ermiq · Mar 21, 2021 at 05:58 AM
Use a cooldown timer. For example:
float coolDown = 0.5f;
float coolDownTimer;
void shootRifle()
{
Instantiate(bulletPrefab, RiflefirePoint.position, RiflefirePoint.rotation);
rifleBulletAmmo -= 1;
coolDownTimer = coolDown;
}
void Update()
{
if (coolDownTimer > 0)
coolDownTimer -= Time.deltaTime;
if (rifleBulletAmmo != 0 && cooldDownTimer <= 0)
canShootRifle = true;
}
Your answer
Follow this Question
Related Questions
so I'm trying to make a 2D platform but the first animation i make is the only one that registers 0 Answers
how to shoot bullets from circumference of a circle ? 1 Answer
How to make animations transition based on players direction and speed ? 0 Answers
How to make a sprite respond to a specific color? 0 Answers
"Dash-Move" only work some times. 1 Answer