Hello! I am new to this so bear with me, I am creating a game for class where I have 3 different weapons and I am creating 3 different bullets for each weapon. The weapons are different shapes, so a SphereGun, CubeGun, and TriangleGun, so each bullet is a different shape. I was able to create a weapon switching system which allows the player to change weapon using scrollwheel up and down, basically enabling the gameObject if it = the index number. However I have bullet shooting attached to my PlayerMovement script where it checks for if the fire key is being pressed and if so will shoot the weapon. I am wondering if there if a way, maybe using if else statements to check if the gameObject = a certain type and if so then instantiate that bullet type prefab? Basically asking if there is a way to change bullet type based on the weapon being held. I will attach my code below.
this is my WeaponSwitching Code
using UnityEngine;
public class WeaponSwitching : MonoBehaviour
{
public int selectedWeapon = 0;
// Start is called before the first frame update
void Start()
{
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousthing = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if (previousthing != selectedWeapon)
{
SelectWeapon();
}
}
void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
}
Here is the bullet shooting aspect of my PlayerMovement script
void Update()
{
if (Input.GetKeyDown("space"))
{
shoot();
}
}
void shoot()
{
GameObject g = Instantiate(bulletsph, shotSpawn.position, shotSpawn.rotation)
as GameObject;
Destroy(g, 1.5f);
}
I am also attaching screenshots of my project hierarchy layout.
Thanks for the help in advance. Please let me know if you have any questions that will help me to find a soluton ![]()
