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.

Gun ammo

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

  • Gun ammo

    I am trying to make it so that when your ammo runs to zero, you stop shooting and the print says "out of ammo" Ive gotten the ammo to work on the UI but it keeps shooting after zero and "out of ammo" keeps showing up before I run out.

    Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class GunScript : MonoBehaviour
    {[INDENT]public Rigidbody grenade;
    public int projectileSpeed;
    public int ammo;
    public Text ammoValue;
    
    
    void Update()[/INDENT][INDENT]{[/INDENT][INDENT]if (Input.GetButtonDown("Fire1"))[/INDENT][INDENT=2]{[/INDENT][INDENT=3]Rigidbody clonedBall;
    clonedBall = Instantiate(grenade, transform.position, transform.rotation);
    clonedBall.velocity = transform.TransformDirection(Vector3.forward * projectileSpeed);
    
    if (ammo >= 0)
    {[/INDENT][INDENT=4]ammo--;
    ammoValue.text = ammo.ToString();
    print("out of ammo");[/INDENT][INDENT=3]}[/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
     
     }

  • #2
    You have code telling it to say out of ammo but nothing to tell it to stop producing ammo.

    Code:
    Rigidbody clonedBall;
    clonedBall = Instantiate(grenade, transform.position, transform.rotation);
    clonedBall.velocity = transform.TransformDirection(Vector3.forward * projectileSpeed);
    This should be in its own if statement inside of if (Input.GetButtonDown("Fire1"))

    Like this:

    Code:
     if (ammo > 0)
     {
               Rigidbody clonedBall;
    clonedBall = Instantiate(grenade, transform.position, transform.rotation);
    clonedBall.velocity = transform.TransformDirection(Vector3.forward * projectileSpeed);
                ammo = ammo-1;
                    ammoUI.text = ammo.ToString();
                }
    That code is now telling it to check if ammo is greater than zero then, if it is, it is going to fire a ball, decrease ammo by one and change the UI to reflect the new change. If ammo is at zero, this code won't do any work and you won't be able to shoot anything.


    Also, you're telling it to print "Out of ammo" if ammo is greater than zero. If you want it to print that when you run out it would be ammo <= 0.

    Hope this helped! I had to do some adjusting from how mine is organized to fit the way you have yours.

    Comment


    • #3
      For some reason, the UI wont change..

      Comment


      • #4
        So, I'm not sure where your code is now...it looks like you've got some other suggestions from Sarai, but your original problem:

        I am trying to make it so that when your ammo runs to zero, you stop shooting and the print says "out of ammo" Ive gotten the ammo to work on the UI but it keeps shooting after zero and "out of ammo" keeps showing up before I run out.
        Let's look at what your code does. If I wrote your code in pseudocode, it'd look like this:
        Code:
        if (someone preses the Fire1 button)
        {
             clone the ball;
             give it velocity;
        
             if(ammo is ***greater than (or equal to)*** 0)
             {
                  subtract one from ammo count;
                  write the value of ammo to the UI;
                  print "out of ammo"
             }
        }
        So, why would we print "out of ammo when ammo was greater than 0? We only want it to show out of ammo when ammo is less than 0.

        Think of structuring it more like this:

        Code:
        if (someone presses the Fire1 button)
        {
             if (ammo is greater than 0)
             {
                  clone the ball;
                  give it velocity;
                  subtract one from ammo count;
                  write the value of ammo to the UI;
             }
        
             if(ammo is less than (or equal to) 0)
             {
        
                  print "out of ammo"
                  or change the UI to show the player they can't fire
             }
        }
        What this would do is if the player presses Fire1, but ammo is not greater than 0, it won't instantiate the ball....

        Comment


        • #5
          Oh, thank you very much. It is working as expected now. Ill stay on this thread in case I have anymore trouble.

          Comment


          • #6
            Now im having some trouble with the trying to keep the score. im able to get each value from hitting the target, but it doesnt add up. and the highscore wont change either.
            Code:
            using System.Collections;
            using System.Collections.Generic;
            using UnityEngine;
            using UnityEngine.UI;
            
            public class PointsScript : MonoBehaviour
            {[INDENT]public int points;
            public GameObject scoreValue;
            
            private void OnCollisionEnter(Collision collision)
            {[/INDENT][INDENT=2]points = +(points);
            scoreValue.GetComponent<Text>().text = points.ToString();[/INDENT][INDENT]}[/INDENT]
             }

            Comment


            • #7
              Go back and check your notes from Pachinko. You need one place to keep the score. You need a ScoreKeeper, a ScoreKeeperScript, and then each of your targets will pass their value to the ScoreKeeper.

              Comment


              • #8
                On the end screen, the highscore appears but the regular score doesnt. It only shows zero.
                Code:
                using System.Collections;
                using System.Collections.Generic;
                using UnityEngine;
                using UnityEngine.UI;
                public class EndScoreScript : MonoBehaviour
                {[INDENT]public int score;
                public Text scoreUI;
                public int highScore;
                
                public void Start()
                {[/INDENT][INDENT=2]score = PlayerPrefs.GetInt("Score");
                GameObject.Find("ScoreValue").GetComponent<Text>() .text = score.ToString();
                highScore = PlayerPrefs.GetInt("HighScore");
                GameObject.Find("HiScoreValue").GetComponent<Text> ().text = highScore.ToString();[/INDENT][INDENT]}[/INDENT]
                  }
                Last edited by Michael Plueger; October 20, 2020, 21:10.

                Comment


                • #9
                  So I can see where you're getting the ScoreValue in this script. But are you setting the ScoreValue in the GameLevel?

                  Comment


                  • #10
                    I figured it out!

                    Comment

                    Working...
                    X