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.

Water balloons aren't being counted in the enemys health UI

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

  • Water balloons aren't being counted in the enemys health UI

    Whenever the enemy is hit by a water balloon and water gun, it decreases its health and is supposed to be displayed in the UI above their head. However, when I switch from the gun to the water balloons, the water balloon hits aren't being displayed by the UI anymore. I'm using a switch case for the UI health and I think the reason its not counting the water balloon hits is because the water balloon has a value of 20 while the water particles have a value of 2. So when I switch back to the balloons after using the water gun, the water balloon values don't match up with the case values, skipping it from working.

    Is there a different way to do the health UI without the switch case? or is there a way to tell the switch case to know about the numbers in between?

    WaterBalloonEnemy Script

    Code:
    public class BalloonThrowerHealth : MonoBehaviour
    {
        public int health;
        Animator myAnimator;
        public GameObject dummyBalloon;
        Image soakBar;
        public Transform soakImage;
        public GameObject soakPoint;
        Image wetBar;
        public Transform wetImage;
        Image healthBar;
        public Transform barImage;
    
        public Transform player;
    
        int test;
    
        void Start()
        {
    
        }
    
        private void Awake()
        {
            myAnimator = GetComponent<Animator>();
    
            wetBar = GameObject.Find("wet").GetComponent<Image>();
            soakBar = GameObject.Find("BalloonEnemyDry").GetComponent<Image>();
            healthBar = GameObject.Find("BalloonHealthBar").GetComponent<Image>();
    
            soakPoint.SetActive(true);
    
            test = 1500;
        }
    
        public void DoDamage(int damageAmount)
        {
            health = health - damageAmount;
            print("enemy health is" + health);
    
            test = test - damageAmount;
    
            switch(test)
            {
                case 1500:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(100f, 100f);
                    break;
                case 1400:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(93.34f, 100f);
                    break;
                case 1300:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(86.68f, 100f);
                    break;
                case 1200:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(80.02f, 100f);
                    break;
                case 1100:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(73.36f, 100f);
                    break;
                case 1000:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(66.7f, 100f);
                    break;
                case 900:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(60.04f, 100f);
                    break;
                case 800:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(53.38f, 100f);
                    break;
                case 700:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(46.72f, 100f);
                    break;
                case 600:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(40.06f, 100f);
                    break;
                case 500:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(33.4f, 100f);
                    break;
                case 400:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(26.74f, 100f);
                    break;
                case 300:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(20.08f, 100f);
                    break;
                case 200:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(13.42f, 100f);
                    break;
                case 100:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(6.76f, 100f);
                    break;
                case 0:
                    soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(0f, 1000f);
                    break;
            }
    
            if(health <= 0 && test <= 0)
            {
                dummyBalloon.SetActive(false);
                myAnimator.SetTrigger("gotSoaked");
    
                soakPoint.SetActive(false);
    
                Destroy(GetComponent<BallonThrowAI>());
                Destroy(GetComponent<Collider>());
            }
        }
    
        private void Update()
        {
            barImage.LookAt(player);
            soakImage.LookAt(player);
            wetImage.LookAt(player);
        }

  • #2
    I'm not sure why you've got a value for "health" and another for "test". It doesn't look like you ever use or need "health". You probably should clean that part up and make the code simpler (although it's probably not what's breaking your code)

    Generally, your CaseSwitch there is in good shape (although you still need a default case). So I would suspect the problem is elsewhere.

    I'd try this: since "test" is public, play the game and don't worry about the UI; just check to see if the test value is changing for both the gun and waterbaloon. If not, there's probably a problem with whatever is sending the message to this scrip to fire its DoDamage() function.

    Comment


    • #3
      I cleaned up the script so that test could be health instead. I played the game and the values are changing correctly whenever both the water balloons and water gun hit the enemy, so I'm still not sure how to resolve this issue.

      Here's the WaterBalloonEnemy script:

      Code:
      {
          public int health;
          Animator myAnimator;
          public GameObject dummyBalloon;
          Image soakBar;
          public Transform soakImage;
          public GameObject soakPoint;
          Image wetBar;
          public Transform wetImage;
          Image healthBar;
          public Transform barImage;
      
          public Transform player;
      
          void Start()
          {
      
          }
      
          private void Awake()
          {
              myAnimator = GetComponent<Animator>();
      
              wetBar = GameObject.Find("wet").GetComponent<Image>();
              soakBar = GameObject.Find("BalloonEnemyDry").GetComponent<Image>();
              healthBar = GameObject.Find("BalloonHealthBar").GetComponent<Image>();
      
              soakPoint.SetActive(true);
          }
      
          public void DoDamage(int damageAmount)
          {
              health = health - damageAmount;
              print("enemy health is" + health);
      
              switch(health)
              {
                  case 1500:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(100f, 100f);
                      break;
                  case 1400:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(93.34f, 100f);
                      break;
                  case 1300:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(86.68f, 100f);
                      break;
                  case 1200:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(80.02f, 100f);
                      break;
                  case 1100:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(73.36f, 100f);
                      break;
                  case 1000:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(66.7f, 100f);
                      break;
                  case 900:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(60.04f, 100f);
                      break;
                  case 800:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(53.38f, 100f);
                      break;
                  case 700:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(46.72f, 100f);
                      break;
                  case 600:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(40.06f, 100f);
                      break;
                  case 500:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(33.4f, 100f);
                      break;
                  case 400:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(26.74f, 100f);
                      break;
                  case 300:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(20.08f, 100f);
                      break;
                  case 200:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(13.42f, 100f);
                      break;
                  case 100:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(6.76f, 100f);
                      break;
                  case 0:
                      soakBar.GetComponent<RectTransform>().sizeDelta = new Vector3(0f, 1000f);
                      break;
                  default:
                      break;
              }
      
              if(health <= 0)
              {
                  dummyBalloon.SetActive(false);
                  myAnimator.SetTrigger("gotSoaked");
      
                  soakPoint.SetActive(false);
      
                  Destroy(GetComponent<BallonThrowAI>());
                  Destroy(GetComponent<Collider>());
              }
          }
      
          private void Update()
          {
              barImage.LookAt(player);
              soakImage.LookAt(player);
              wetImage.LookAt(player);
          }
      The Water Balloon Collision Script:

      Code:
      {
          public GameObject splash;
          private void OnCollisionEnter(Collision collision)
          {
              if(collision.collider.gameObject.name == "FPSController")
              {
                  collision.collider.gameObject.GetComponent<PlayerHealthScript>().DoDamage(20);
      
              }
      
              if(collision.collider.gameObject.tag == "BalloonThrower")
              {
                  collision.collider.gameObject.GetComponent<BalloonThrowerHealth>().DoDamage(20);
              }
      
              if(collision.collider.gameObject.tag == "WaterGunner")
              {
                  collision.collider.gameObject.GetComponent<WaterGunnerHealth>().DoDamage(20);
              }
      
              Instantiate(splash, transform.position, transform.rotation);
              Destroy(gameObject);
          }
      }

      Comment


      • #4
        OK. So yes, that's cleaner code.

        I'm not sure what the problem is, but here's what I'd guess:

        Your water gun is firing like a machine gun so there are many collisions happening very quickly. Even with a damage amount at 2, those still add up in a hurry. Your water balloon damage is 20, but your case switch is changing only when things change by 200 or more. Which means you'd have to hit the character with 10 water balloons before it moves to the next case.

        In your WaterBalloonCollisionScript, try changing the damage amount to something much bigger. Try: DoDamage(200); or something like that and see if it budges your UI.

        Comment

        Working...
        X