Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dragunas · Feb 10, 2021 at 12:25 PM · detectionweapon

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++;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check Objects on each side! 1 Answer

How to find the portion of a 3D object that is exposed from a specific angle? 1 Answer

Trigger Detecting Player Even When Not Inside 1 Answer

Trying to make a procedurally generated dungeon (3D), but rooms won't spawn 1 Answer

picking up guns 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges