So i’m on the very early stages of a project i’m working on and fairly new to coding and unity (not pushed a final project but have worked on it for some time)
I am having some concerns right now as well as confused on the best way to call for some code.
A majority of my code is using if statements and since the project is in the early stages I havent seen any slowdown or problems due to the uses of if’s(barely any nested loops). But my major concern is wondering if i’m using to many if statements as well as when is a good time to use a switch statement?
I’m working on an ammo changing method on the gun (thinking I should switch it to the player) and using this file
using UnityEngine;
public class Gun : MonoBehaviour
{
public bool gunIsHeld;
[SerializeField]
private Transform firePoint;
public GameObject normalBulletPref;
public GameObject darkBulletPref;
public GameObject lightBulletPref;
public float bulletForce = 20;
//bullet types
[SerializeField]
private bool isNormal;
[SerializeField]
private bool isDark;
[SerializeField]
private bool isLight;
//gun rotation;
private Rigidbody2D rb;
Vector2 mousePos;
[SerializeField]
private Transform playerPos;
public float FireRateMax;
public float FireRateMin;
private void Start()
{
firePoint = GetComponentInChildren<Transform>();
rb = GetComponent<Rigidbody2D>();
FireRateMin = FireRateMax;
isNormal = true;
}
public void Update()
{
//checks that the gun is being held by the player;
IsHeld();
if(gunIsHeld)
{
if (Input.GetMouseButtonDown(1))
{
AmmoTypeSwitch();
}
}
if (gunIsHeld)
{
transform.position = playerPos.position;
//start calling for the rotation of the gun before shooting
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//if left click start shooting
if (Input.GetMouseButton(0))
{
Shoot();
}
}
}
private void FixedUpdate()
{
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
rb.rotation = angle;
}
public void Shoot()
{
if(isNormal == true)
{
if (FireRateMin == FireRateMax)
{
GameObject bullet = Instantiate(normalBulletPref, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.right * bulletForce, ForceMode2D.Impulse);
FireRateMin -= Time.deltaTime * 1;
}
else if (FireRateMin != FireRateMax)
{
FireRateMin -= Time.deltaTime * 1;
if (FireRateMin <= 0)
{
FireRateMin = FireRateMax;
}
}
}
else if(isDark == true)
{
if (FireRateMin == FireRateMax)
{
GameObject bullet = Instantiate(darkBulletPref, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.right * bulletForce, ForceMode2D.Impulse);
FireRateMin -= Time.deltaTime * 1;
}
else if (FireRateMin != FireRateMax)
{
FireRateMin -= Time.deltaTime * 1;
if (FireRateMin <= 0)
{
FireRateMin = FireRateMax;
}
}
}
else if(isLight == true)
{
if (FireRateMin == FireRateMax)
{
GameObject bullet = Instantiate(lightBulletPref, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.right * bulletForce, ForceMode2D.Impulse);
FireRateMin -= Time.deltaTime * 1;
}
else if (FireRateMin != FireRateMax)
{
FireRateMin -= Time.deltaTime * 1;
if (FireRateMin <= 0)
{
FireRateMin = FireRateMax;
}
}
}
}
void AmmoTypeSwitch()
{
if (isNormal == true)
{
isNormal = false;
isDark = true;
isLight = false;
Debug.Log("Using Dark Ammo");
}
else if (isDark == true)
{
isNormal = false;
isDark = false;
isLight = true;
Debug.Log("Using light Ammo");
}
else if (isLight == true)
{
isNormal = true;
isDark = false;
isLight = false;
Debug.Log("Using normal Ammo");
}
}
void IsHeld()
{
if (gameObject.transform.parent == null)
{
gunIsHeld = false;
rb.isKinematic = true;
}
else if(gameObject.transform.parent.CompareTag("Player"))
{
gunIsHeld = true;
rb.isKinematic = false;
}
}
}
I’m not sure if if statements will be the best to use in the AmmoTypeSwitch() method or if I should change it to a switch.
The code works but as a fairly new programmer I will still like to learn the best practices so I can grow into a better coder.
P.S. Lastly just wanted to know if it would be better to have the current ammo type on the player and have the gun call from the player to check.
Thank you!