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 armandas2005 · Mar 07, 2021 at 07:49 AM · weapondrop

Fire rate of different weapons after being dropped

I made two weapon types, one is a machine gun and the other one is a pistol and then made an drop and pick up system but when i drop the machine gun and select my pistol it starts shooting like the machine gun.

here is my weapon scrip:

 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;
 
     private InventoryInput inventoryInput;
 
     public bool isMachineGun;
 
     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()
     {
         inventoryInput = FindObjectOfType<InventoryInput>();
 
         pickUp = FindObjectOfType<PickUp>();
 
         audSource = GetComponent<AudioSource>();
 
         currentAmmo = maxAmmo;
 
         ShootEnable = true;
     }
 
     void OnEnable()
     {
         isReloading = false;
     }
 
     void Update()
     {
         fpsCam = GameObject.Find("/FirstPersonPlayer/MainCamera").GetComponent<Camera>();
 
         if (currentAmmo <= 0)
         {
             ammoEmpty = true;
         }
         else
         {
             ammoEmpty = false;
         }
 
         if (Input.GetKeyDown(KeyCode.L))
         {
             Ammo += 10;
         }
 
         if (Input.GetKeyDown(KeyCode.R) && currentAmmo != maxAmmo && Ammo != 0)
         {           
                 StartCoroutine(Reload());
                 return;            
         }
 
         if(currentAmmo <= 0)
         {
             ShootEnable = false;
         }
 
         if(currentAmmo >= 0)
         {
             ShootEnable = true;
         }
 
         if (Input.GetButtonDown("Fire1") && ShootEnable == true && ammoEmpty == false && isReloading == false && inventoryInput.open == false && isMachineGun == false)
         {
             Shoot();
         }
 
         if (Input.GetKey("Fire1") && ShootEnable == true && ammoEmpty == false && isReloading == false && inventoryInput.open == false && isMachineGun == true)
         {
             Shoot();
         }
 
         //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);
             }
         }
     }
 }

here is my Pick up/drop script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PickUp : MonoBehaviour
 
 {
     private Weapon weapon;
 
     public bool reloadSound;
 
     public float transformX = 0.15f;
     public float transformY = 0.15f;
     public float transformZ = 0.15f;
 
     public Weapon gunScript;
     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()
     {
         weapon = GameObject.FindObjectOfType<Weapon>();
     }
 
     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()
     {
         if (player == null)
         {
             player = transform.Find("/FirstPersonPlayer");
         }
 
         if (gunContainer == null)
         {
             gunContainer = transform.Find("/FirstPersonPlayer/MainCamera/GunContainer");
         }
 
         if (fpsCam == null)
         {
             fpsCam = transform.Find("/FirstPersonPlayer/MainCamera");
         }
 
         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);
     }
 
 }
 
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Captain_Pineapple · Mar 07, 2021 at 10:24 AM

You mixed up GetComponent and FindObjectOfType The first will get the a script of the type in question on the same gameobject. The altter will return just the first instance of the type in question on any object in the scene. So just change that in your pickup script and you should be fine.


just as an addition: Remove these

 if (player == null)
          {
              player = transform.Find("/FirstPersonPlayer");
          }
  
          if (gunContainer == null)
          {
              gunContainer = transform.Find("/FirstPersonPlayer/MainCamera/GunContainer");
          }
  
          if (fpsCam == null)
          {
              fpsCam = transform.Find("/FirstPersonPlayer/MainCamera");
          }

from the update and add it to the Start()function of your pickup script. If you'd have lets say 50 guns on the ground they'd do a total of 150 null checks each frame all the time for no reason at all.

Comment
Add comment · Share
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

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

115 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 avatar image avatar image avatar image

Related Questions

How to use rigidbody.AddForce in a single frame. 2 Answers

Where could I get good house/weapon textures? 1 Answer

Weapon pick up and switching script 2 Answers

How to make a weapon index? 1 Answer

FPS Weapon and animation 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