- Home /
How to make UI slider decrease in increments by 0.1f as each bullet is shot. using currentBullets--;
Hello im trying to make it so when i double click and shoot a bullet i have set up in my script it decreases the slider by 0.1 each time. Here is my 2 scripts.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaterTap : MonoBehaviour {
public GameObject gib;
public bool singleTap = false;
public bool doubleTap = false;
public bool canactuallyTap = true;
public float speed = 5.0f;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
FlyingSliderMG fly;
public AudioClip[] clips;
public AudioSource source;
bool tapping = false;
float tapTime = 0;
float duration = .4f;
//public Collider2D coll;
public float waitRate = 0.2f;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
//coll = GetComponent<Collider2D>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
canactuallyTap = true;
if (canactuallyTap)
if (tapping)
{
DashSound();
doubleTap = true;
nextFire = Time.time + fireRate;
speed = 30f;
fireRate = 0.20f;
//GetComponent<SpriteRenderer>().color = Color.red;
nextFire = Time.time + fireRate;
Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
Vector2 myPos = new Vector2(transform.position.x + 0.5f, transform.position.y);
Vector2 direction = target - myPos;
direction.Normalize();
Quaternion rotation = Quaternion.Euler(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);
GameObject projectile = Instantiate(gib, myPos, rotation);
projectile.GetComponent<Rigidbody2D>().velocity = direction * speed;
Debug.Log("DoubleTapReached");
//fly.currentBullets--;
fly.airTime -= 1; /** Time.deltaTime*/
fly.slider.value = fly.airTime;
tapping = false;
//rigidbody2D.transform.position + clone. * 10.0f
//rigidbody2D.AddForce(transform.forward * 100);
//gib.GetComponent<Rigidbody2D>().velocity = transform.forward * 100;
//GetComponent<Collider2D>().isTrigger = false;
}
else
{
tapping = true;
tapTime = duration;
}
}
if (tapping)
{
tapTime = tapTime - Time.deltaTime;
if (tapTime <= 0)
{
tapping = false;
singleTap = true;
Debug.Log("SingleTap");
}
}
}
void LateUpdate()
{
if (doubleTap) doubleTap = false;
if (singleTap) singleTap = false;
}
//IEnumerator WaitAMin()
//{
// GetComponent<Collider2D>().isTrigger = false;
// gameObject.layer = 27;
// yield return new WaitForSeconds(waitRate);
// gameObject.layer = 9;
// GetComponent<Collider2D>().isTrigger = true;
//}
public void DashSound()
{
source.clip = clips[Random.Range(0, clips.Length)];
source.Play();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FlyingSliderMG : MonoBehaviour {
public Slider slider;
private GameObject player;
public float airTime = 1.0f;
public bool spaceBarNotPressed = false;
public float timeDecrease = 1.0f;
public bool canShootFrost = false;
public float timeIncrease = 1.0f;
public int bulletsPerMag = 30;
public int currentBullets;
public int bulletsLeft;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
public bool isReloading;
public float air = 1.0f;
public float speed = 1.0f;
void Awake()
{
GetReferences();
}
void Start()
{
currentBullets = bulletsPerMag;
}
void GetReferences()
{
player = GameObject.Find("Player");
slider = GameObject.Find("TimerSlider").GetComponent<Slider>();
slider.minValue = 0f;
slider.maxValue = airTime;
slider.value = slider.maxValue;
}
// Use this for initialization
void Update()
{
if (currentBullets > 0)
Fire();
else if (bulletsLeft > 0)
StartReload();
//if (!player)
// return;
//if (Input.GetButton("Space"))
//{
// if (airTime > 0)
// {
// airTime -= timeDecrease * Time.deltaTime;
// slider.value = airTime;
// if (airTime < 0)
// {
// GameObject.Find("LevelManager").GetComponent<FlyingSliderMG>().airTime += 10f;
// }
// }
//}
}
public void Fire()
{
if (nextFire < fireRate || currentBullets <= 0 || isReloading)
return;
}
public void Reload()
{
if (bulletsLeft <= 0) return;
int bulletToLoad = bulletsPerMag - currentBullets;
int bulletsToDeduct = (bulletsLeft >= bulletToLoad) ? bulletToLoad : bulletsLeft;
bulletsLeft -= bulletsToDeduct;
currentBullets += bulletsToDeduct;
}
private void StartReload()
{
if (isReloading) return;
}
IEnumerator IncreaseFlyRateIfZero()
{
while (true)
if (airTime <= 0)
{
yield return new WaitForSeconds(0.06f);
airTime += 10.0f * Time.deltaTime;
slider.value = airTime;
}
else {
yield return null;
}
}
IEnumerator IncreaseFlyRateOverall()
{
yield return new WaitForSeconds(0.06f);
airTime += timeDecrease * Time.deltaTime;
slider.value = airTime;
}
}
the slider decreases once each time a bullet is fired so boom 1 shot decrease.
another shot and it decreases so every time i fire it decreases by a certain amount
Why don't you make your slider use whole numbers, set the $$anonymous$$ to 0 and the max to bulletsPer$$anonymous$$ag?
Answer by shadowpuppet · Nov 16, 2017 at 08:11 AM
why? I am of no help because the gun/bullet system I have set up is an int. Not sliders. bullets are physical entities that can be monitored by integers and I track that by other means ( not a float on a slider ,but , as an int through GUI graphic representation
I wanted it to be a slider for the ammo indication to be high tech + $$anonymous$$imal so ins$$anonymous$$d of showing the ammo number I display it as a slider.