- Home /
How to detect wich weapon equipped and select method
i made two weapon types in my game, a machine gunn and a pistol. I want my pistol to shoot normal and my machine gun should be full auto. but how can i make it so that my script detects wich weapon i have selected and change between auto and normal? sorry for my bad english by the way
Weapon script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Weapon : MonoBehaviour { public float damage = 10f; public float range = 100f; public Text ammodisplay;
public int maxAmmo = 10;
public int Ammo = 35;
public bool ammoEmpty;
public GameObject gunModel;
private PickUp pickUp;
public Text ammoText;
public float aimSpeed = 4;
public AudioClip gunShotClip;
public AudioClip reloadClip;
public AudioSource audSource;
public int currentAmmo;
public float reloadTime = 5f;
private bool isReloading = false;
public float recoilStrength = 2f;
public Camera fpsCam;
public Vector3 normalPosition;
public Vector3 aimingPosition;
public bool ShootEnable = true;
public bool isAiming = false;
// Update is called once per frame
void Start()
{
pickUp = FindObjectOfType<PickUp>();
audSource = GetComponent<AudioSource>();
currentAmmo = maxAmmo;
ShootEnable = true;
}
void OnEnable()
{
isReloading = false;
}
void Update()
{
ammoText.text = ("Ammo: " + currentAmmo + "/" + Ammo);
if (currentAmmo <= 0)
{
ammoEmpty = true;
}
else
{
ammoEmpty = false;
}
if (Input.GetKeyDown(KeyCode.L))
{
Ammo += 10;
}
if (Input.GetKeyDown(KeyCode.R) && currentAmmo != maxAmmo && Ammo != 0 && isReloading == false)
{
StartCoroutine(Reload());
return;
}
if(currentAmmo <= 0)
{
ShootEnable = false;
}
if(currentAmmo >= 0)
{
ShootEnable = true;
}
if (Input.GetButtonDown("Fire1") && ShootEnable == true && ammoEmpty == false && isReloading == false) //&& != MachineGun
{
Shoot();
}
//if (Input.GetButton("Fire1") && ShootEnable == true && ammoEmpty == false && isReloading == false && != Pistol)
//{
// MachineGunShoot();
//}
//ammodisplay.text = ("Munition:") + currentAmmo.ToString();
if (Input.GetKey(KeyCode.Mouse1))
{
isAiming = true;
}
else if (!Input.GetKey(KeyCode.Mouse1))
{
isAiming = false;
}
}
private void FixedUpdate()
{
if (isAiming == true)
{
gunModel.transform.localPosition = Vector3.Lerp(gunModel.transform.localPosition, aimingPosition, Time.deltaTime * aimSpeed);
}
else if (isAiming == false)
{
gunModel.transform.localPosition = Vector3.Lerp(gunModel.transform.localPosition, normalPosition, Time.deltaTime * aimSpeed);
}
}
public IEnumerator Reload()
{
audSource.PlayOneShot(reloadClip);
isReloading = true;
yield return new WaitForSeconds(reloadTime);
int totalAmmo = currentAmmo + Ammo;
if (totalAmmo <= maxAmmo)
{
currentAmmo = totalAmmo;
Ammo = 0;
}
else
{
int shots = maxAmmo - currentAmmo;
currentAmmo = 10;
Ammo -= shots;
}
isReloading = false;
}
void Shoot()
{
audSource.PlayOneShot(gunShotClip);
currentAmmo--;
Vector3 gunModelLocalPosition = gunModel.transform.localPosition;
gunModelLocalPosition.z = gunModelLocalPosition.z - recoilStrength;
gunModel.transform.localPosition = gunModelLocalPosition;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Leben leben = hit.transform.GetComponent<Leben>();
if (leben != null)
{
leben.TakeDamage(damage);
}
}
}
}
PickUp Script
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PickUp : MonoBehaviour
{ private Weapon weapon;
private ItemDataBase itemDataBase;
private ItemObj itemObj;
private ToolTipButton toolTipB;
public bool reloadSound;
public float transformX = 0.15f;
public float transformY = 0.15f;
public float transformZ = 0.15f;
public Weapon gunScript;
public WeaponM machinegunScript;
public Rigidbody rb;
public BoxCollider coll;
public Transform player, gunContainer, fpsCam;
public float pickUpRange;
public float dropForwardForce, dropUpwardForce;
private Inventory inventory;
public bool equipped;
public static bool slotFull;
void Awake()
{
toolTipB = GameObject.FindObjectOfType<ToolTipButton>();
weapon = GameObject.FindObjectOfType<Weapon>();
inventory = GameObject.FindObjectOfType<Inventory>();
itemObj = GameObject.FindObjectOfType<ItemObj>();
}
private void Start()
{
if (!equipped)
{
gunScript.enabled = false;
rb.isKinematic = false;
coll.isTrigger = false;
}
if (equipped)
{
gunScript.enabled = true;
coll.isTrigger = true;
slotFull = true;
rb.isKinematic = true;
}
}
private void Update()
{
Vector3 distanceToPlayer = player.position - transform.position;
if (!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKeyDown(KeyCode.E)) pickUp();
if (equipped && Input.GetKeyDown(KeyCode.Q)) Drop();
}
public void pickUp()
{
reloadSound = true;
equipped = true;
slotFull = true;
transform.SetParent(gunContainer);
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.Euler(Vector3.zero);
transform.localScale = new Vector3(transformX, transformY, transformZ);
rb.isKinematic = true;
coll.isTrigger = true;
gunScript.enabled = true;
// weapon.ammodisplay.gameObject.SetActive(true);
}
public void Drop()
{
reloadSound = false;
equipped = false;
slotFull = false;
transform.SetParent(null);
rb.isKinematic = false;
coll.isTrigger = false;
rb.AddForce(fpsCam.forward * dropForwardForce, ForceMode.Impulse);
rb.AddForce(fpsCam.up * dropUpwardForce, ForceMode.Impulse);
float random = Random.Range(-1f, 1f);
rb.AddTorque(new Vector3(random, random, random)* 10);
gunScript.enabled = false;
//weapon.ammodisplay.gameObject.SetActive(false);
}
}
Select weapon script(i got this from Brackeys) using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WeaponSwitching : MonoBehaviour { public int selectedWeapon = 0; private Inventory inventory;
// Start is called before the first frame update
void Start()
{
inventory = GameObject.FindObjectOfType<Inventory>();
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;
if(Input.GetAxis("Mouse ScrollWheel") > 0f && inventory.isShown == false)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f && inventory.isShown == false)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if (Input.GetKeyDown(KeyCode.Alpha1) && inventory.isShown == false)
{
selectedWeapon = 0;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2 && inventory.isShown == false)
{
selectedWeapon = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3 && inventory.isShown == false)
{
selectedWeapon = 2;
}
if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 4 && inventory.isShown == false)
{
selectedWeapon = 3;
}
if (previousSelectedWeapon == 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++;
}
}
}
Your answer

Follow this Question
Related Questions
Weapon Mesh Damage 1 Answer
weapon bob when looking 2 Answers
Delay child objects rotation 0 Answers
Wait For Seconds c# 3 Answers
Network bullet issue 2 Answers