Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mydevaccs · May 25, 2018 at 09:24 PM · booleanweaponsetactivemuzzleflash

Why is this returning false but somewhat true?

So I am working on a script to fire a weapon. I have an if statement: if(Input.GetMouseButton(0) && currentAmmo > 0){ Now if my currentAmmo is 40 and the mouse is held down then the why does the Debug.Log("Stop") get called soon as the game starts and constantly gets called even while I'm holding down left mouse and when I'm not..

The issue with this code is that the muzFX (Muzzle flash) isn't being set active.. However, the audio is. Here is the rest of the code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 /// <summary>
 ///    This is the Weapon Controller
 /// This script handles controlling the 
 /// current weapon based on the current
 /// weapons profile.
 /// </summary>
 
 public class WeaponController : MonoBehaviour {
 
 
     [Header("Weapon Parameters")]
     [SerializeField]
     private GameObject currentWeapon;
     
     [SerializeField]
     private GameObject hitFX;
 
     [SerializeField]
     private WeaponProfile weaponProfile;
 
     [SerializeField]
     private float currentAmmo;
 
     [SerializeField]
     private GameObject muzFX;
 
     [SerializeField]
     private AudioSource audioSource;
 
     // [Header("AI Settings")]
     // [SerializeField]
     // private AIController ai;
 
     void Start()
     {
         // Chache references
         weaponProfile = currentWeapon.GetComponent<WeaponProfile>();
         audioSource = weaponProfile.soundFX;
 
 
         // muzFX = GameObject.Find("XFlash");
         muzFX.SetActive(false);
 
         // Fill the first magazine and subtract that
         // from the maximum ammo. (This allows running out of ammo)
         currentAmmo = weaponProfile.magCapacity;
         weaponProfile.ammoCapacity -= (int)currentAmmo;
     }
 
     public void FireWeapon(){
 
         // if(!weaponProfile.isMeleeWeapon){
 
         // If we have ammo and we are firing
         // if(currentAmmo >= 0 ){// }
         if(Input.GetMouseButton(0) && currentAmmo > 0){
     
             // Check if we have a muzzle flash selected
             // and set it to active
             if(muzFX != null) {
                 muzFX.SetActive(true);
                 Debug.Log("Show muzzle flash.");
                 if(!audioSource.isPlaying){
                     audioSource.Play();    
                 }
 
                 
                 Debug.Log("Playing audio: " + audioSource);
                 
                 // Reduce current ammo by the weapon profile's firerate
                 Debug.Log("Firing Weapon..");
                 currentAmmo -= 1 * weaponProfile.fireRate * Time.deltaTime;
 
                 // Gather center of screen Ranging from 0f to 1.0f on X/Y Axis
                 // and cast a raycast. Instantiating the hitFX on point of contact.
                 Ray _rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
                 RaycastHit hitInfo;
                 if(Physics.Raycast(_rayOrigin, out hitInfo, Mathf.Infinity)){
                     Debug.Log("Raycast hit: " + hitInfo.transform.name);
                     Instantiate(hitFX, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
 
                     // if(hitInfo.transform.tag == "AI"){
                     //     ai = hitInfo.transform.GetComponent<AIController>();
                     //     ai.TakeDamage(weaponProfile.damage);
                     //     Debug.Log("Applied: " + weaponProfile.damage + " damage to: " + hitInfo.transform.name);
                     // }
                 }
             }else { Debug.LogError("muzFlash is null");}
         }else { 
             muzFX.SetActive(false);
             Debug.Log("Stop.");
             audioSource.Stop();
         }
         
     } 
 
     public void EnableReload(){
         // [FIX NEEDED]
         // If the magazine is out of ammo, and there is still ammo left
         // reload the clip, subtract that ammo from our max ammo. This isn't 100% accruate
         // and leaves a few bullets unusable. 
         if(currentAmmo <= 0 && weaponProfile.ammoCapacity >= weaponProfile.magCapacity){
             if (Input.GetKeyDown(KeyCode.R))
             {
                 currentAmmo = weaponProfile.magCapacity;
                 weaponProfile.ammoCapacity  -= (int)currentAmmo;
             }
         }
     }
     
 }
 

Comment
Add comment · Show 1
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
avatar image winterfluxstudio · May 25, 2018 at 10:08 PM 0
Share

You're doing some weird stuff (or not doing some stuff, as it were) you havent initialised currentWeapon, or muzFX, even though you have references to them. Because they are private, you need to initialise them.

 private GameObject currentWeapon;
 private GameObject muzFX;
 
 void Start()
 {
     currentWeapon = GameObject.FindWithTag("CurrentWeapon");
     muzFX = etc.....
 }


Assu$$anonymous$$g your muzzle flash is a particle system? it needs a specific reference as such (as a particle system, not a game object)

 private ParticleSystem muxFZ;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TanselAltinel · May 25, 2018 at 10:04 PM

I get that you are calling FireWeapon function in Update function.

If I got it right, then it is expected to game to start spamming console in the else as long as you are not pressing left mouse button, because you are in else state.

For your second case, you are decreasing current ammo with this:

 currentAmmo -= 1 * weaponProfile.fireRate * Time.deltaTime;

If your fire rate and delta time multiplication exceeds 1 at any time, casting it to int will decrease your ammo count. I suggest you use Debug.Log(currentAmmo); to see how fast your ammo count decreases. I suspect that it gets zero pretty quickly and you again fall into else state, causing your script to spam console with same message.

If I am right, I suggest you to find a different approach to decrease your current ammo. You can check some official Unity tutorials where ammo count is used, like survival shooter: https://unity3d.com/learn/tutorials/s/survival-shooter-tutorial

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

88 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

Related Questions

How to add Muzzle Flash to Weapon 1 Answer

Array simple problem 1 Answer

play sound only once on SetActive of GameObject 2 Answers

physics.OverlapSphere colliders 1 Answer

Both IF statements in a function are run and their conditions are ignored. 4 Answers


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