- Home /
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;
}
}
}
}
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;
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