Welcome!

Welcome to our community forums, full of great people, ideas and excitement. Please register if you would like to take part.

This is extra text with a test link..

Register Now

Announcement

Collapse
No announcement yet.

Using Case Switch for Balloon Ammo

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Case Switch for Balloon Ammo

    Since the amount of balloons the player can have at one time is small, I felt like it would be more interesting to have balloon sprites that go away to indicate ammo like the health in our viking village project. Everything seems set up fine but I can't seem to plug in the images in the inspector.

    Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class GunScript : MonoBehaviour
    {
        [Header("WaterBalloon")]
    
        public float balloonSpeed;
        public Rigidbody balloon;
        public int balloonAmmo;
        public int maxBalloonAmmo;
        public Text balloonReloadUI;
        public Text balloons;
        public Image balloonIcon1;
        public Image balloonIcon2;
        public Image balloonIcon3;
        public Image balloonIcon4;
    
    
        [Header("WaterGun")]
    
        public Rigidbody ball;
        public Transform gunLP;
        public float ballSpeed;
        public int ammo;
        public int maxAmmo;
        public Text reloadUI;
        RaycastHit thingIHit;
        public RectTransform waterFillBar;
    
        private void Start()
        {
            ammo = maxAmmo;
            balloonAmmo = maxBalloonAmmo;
            reloadUI.gameObject.SetActive(false);
            GameObject.Find("balloonsLeft").GetComponent<Text>().text = balloonAmmo.ToString();
            print(waterFillBar.sizeDelta);
        }
    
    
    
        private void Update()
        {
            if(Input.GetKeyDown(KeyCode.F)&& balloonAmmo>0)
            {
                Rigidbody cloneBalloon;
                cloneBalloon = Instantiate(balloon, gunLP.transform.position, gunLP.transform.rotation);
    
                cloneBalloon.velocity = gunLP.transform.TransformDirection(Vector3.forward * balloonSpeed);
                balloonAmmo = balloonAmmo - 1;
                GameObject.Find("balloonsLeft").GetComponent<Text>().text = balloonAmmo.ToString();
                switch(balloonAmmo)
                {
                    case 3:
                        balloonIcon4.enabled = false;
                        break;
    
                    case 2:
                        balloonIcon3.enabled = false;
    
                        break;
    
                    case 1:
                        balloonIcon2.enabled = false;
                        break;
                    case 0:
                        balloonIcon1.enabled = false;
                        break;
                }
            }
    
    
            if(Input.GetButton("Fire1")&& ammo>0)
            {
                //print("fshhhh");
               //play water sound
    
                Rigidbody cloneBall;
                cloneBall = Instantiate(ball, gunLP.transform.position, gunLP.transform.rotation);
    
                cloneBall.velocity = gunLP.transform.TransformDirection(Vector3.forward * ballSpeed);
                ammo = ammo - 1;
                waterFillBar.sizeDelta = new Vector2(ammo/5, waterFillBar.sizeDelta.y);
            }
    
            if(ammo==0)
            {
                print("Gotta reload!");
            }
    
            if(Input.GetButtonUp("Fire1"))
            {
                //waterSound.Stop();
            }
    
            #region Reload
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out thingIHit, 10))
            {
                Debug.DrawLine(transform.position, thingIHit.point, Color.red);
                //print("I hit" + thingIHit.collider.gameObject);
    
                if (thingIHit.collider.tag == "poolWater")
                {
                    print("yep thats water");
    
    
                    if(ammo<maxAmmo)
                    {
                        print("reload here");
                        reloadUI.gameObject.SetActive(true);
    
                        if (Input.GetButton("Fire3") && ammo<maxAmmo)
                            {
                               ammo = Mathf.Clamp(ammo + 1, 0, maxAmmo);
                            //rebuild reload
                            ammo = ammo + 1;
                            waterFillBar.sizeDelta = new Vector2(ammo / 5, waterFillBar.sizeDelta.y);
                            reloadUI.gameObject.SetActive(false);
                        }
                    }
    
    
                }
    
    
                if (thingIHit.collider.tag == "balloonBucket")
                {
                    print("them be balloons");
    
    
                    if (balloonAmmo < maxBalloonAmmo)
                    {
                        print("reload here");
                        balloonReloadUI.gameObject.SetActive(true);
    
                        if (Input.GetButtonDown("Fire3") && balloonAmmo < maxBalloonAmmo)
                        {
                            balloonAmmo = Mathf.Clamp(balloonAmmo + 1, 0, maxBalloonAmmo);
    
                            balloonAmmo = balloonAmmo + 1;
                            GameObject.Find("balloonsLeft").GetComponent<Text>().text = balloonAmmo.ToString();
                            print("GetBalloon");
                            balloonReloadUI.gameObject.SetActive(false);
                        }
                    }
    
    
                }
            }
            else
            {
                balloonReloadUI.gameObject.SetActive(false);
                reloadUI.gameObject.SetActive(false);
            }
            #endregion
        }
    
    
    
    }

  • #2
    Are those balloons UI Images or UI RawImages?

    Comment


    • #3
      They're UI raw images. Could I just change:

      Code:
      public Image balloonIcon1;
      public Image balloonIcon2;
      public Image balloonIcon3;
      public Image balloonIcon4;
      to raw images or do raw images do something different?

      Comment


      • #4
        Images ≠ Raw Images. The easiest thing to do (so you don't need to change your code) is to make sure your balloons are indeed images (and not RawImages) by making sure the import settings are Sprites.

        Otherwise, in your code you need to change the type to RawImage (https://docs.unity3d.com/2018.3/Docu....RawImage.html).

        Comment

        Working...
        X