• 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 KUFgoddess · Nov 16, 2017 at 07:44 AM · scripting problemsettingsdecrease

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






Comment
Add comment · Show 3
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 KUFgoddess · Nov 16, 2017 at 10:25 AM 0
Share

the slider decreases once each time a bullet is fired so boom 1 shot decrease. alt text

unity-2017-11-16-04-23-00.png (3.3 kB)
avatar image KUFgoddess · Nov 16, 2017 at 10:27 AM 0
Share

another shot and it decreases so every time i fire it decreases by a certain amountalt text

unity-2017-11-16-04-25-51.png (2.4 kB)
avatar image Hellium · Nov 16, 2017 at 10:53 AM 1
Share

Why don't you make your slider use whole numbers, set the min to 0 and the max to bulletsPer$$anonymous$$ag?

1 Reply

· Add your reply
  • Sort: 
avatar image
-2

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

Comment
Add comment · Show 1 · 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
avatar image KUFgoddess · Nov 16, 2017 at 10:22 AM 0
Share

I wanted it to be a slider for the ammo indication to be high tech + minimal so ins$$anonymous$$d of showing the ammo number I display it as a slider.alt text

unity-2017-11-16-04-21-21.png (2.4 kB)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

127 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 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

I don't like Matf.clamp what to do ? 2 Answers

Building Error:Unsafe code may only appear if compiling with /unsafe 0 Answers

Change Physics Contact Pair Modes by code? 1 Answer

Problem with the 2D roguelike GameManager 0 Answers

Disable auto-enable Virtual Reality Support 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges