Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Blackuma · Feb 11, 2016 at 10:55 PM · destroytimerprojectile

Projectile destruction when time reaches zero

I'm making a fps and want enemy projectiles to stop shooting around the last 1 one second of the round so the rigidbody does not activate "play again" buttons at time over. I do have a timer attached to my game manager but I'm having trouble referencing it my projectile shooting script. Is there a way I can change my code to create this reference.

Game Manager

 public class GameManager : MonoBehaviour {
 
     // make game manager public static so can access this from other scripts
     public static GameManager gm;
 
     // public variables
     public int score=0;
 
     public bool canBeatLevel = false;
     public int beatLevelScore=0;
 
     public float startTime=5.0f;
     
     public Text mainScoreDisplay;
     public Text mainTimerDisplay;
 
     public GameObject gameOverScoreOutline;
 
     public AudioSource musicAudioSource;
 
     public bool gameIsOver = false;
 
     public GameObject playAgainButtons;
     public string playAgainLevelToLoad;
 
     public GameObject nextLevelButtons;
     public string nextLevelToLoad;
 
     public float currentTime;
 
     // setup the game
     void Start () {
 
         // set the current time to the startTime specified
         currentTime = startTime;
 
         // get a reference to the GameManager component for use by other scripts
         if (gm == null) 
             gm = this.gameObject.GetComponent<GameManager>();
 
         // init scoreboard to 0
         mainScoreDisplay.text = "0";
 
         // inactivate the gameOverScoreOutline gameObject, if it is set
         if (gameOverScoreOutline)
             gameOverScoreOutline.SetActive (false);
 
         // inactivate the playAgainButtons gameObject, if it is set
         if (playAgainButtons)
             playAgainButtons.SetActive (false);
 
         // inactivate the nextLevelButtons gameObject, if it is set
         if (nextLevelButtons)
             nextLevelButtons.SetActive (false);
     }
 
     // this is the main game event loop
     void Update () {
         if (!gameIsOver) {
             if (canBeatLevel && (score >= beatLevelScore)) {  // check to see if beat game
                 BeatLevel ();
             } else if (currentTime < 0) { // check to see if timer has run out
                 EndGame ();
             } else { // game playing state, so update the timer
                 currentTime -= Time.deltaTime;
                 mainTimerDisplay.text = currentTime.ToString ("0.00");                
             }
         }
     }
 
     void EndGame() {
         // game is over
         gameIsOver = true;
 
         // repurpose the timer to display a message to the player
         mainTimerDisplay.text = "GAME OVER";
 
         // activate the gameOverScoreOutline gameObject, if it is set 
         if (gameOverScoreOutline)
             gameOverScoreOutline.SetActive (true);
     
         // activate the playAgainButtons gameObject, if it is set 
         if (playAgainButtons)
             playAgainButtons.SetActive (true);
 
         // reduce the pitch of the background music, if it is set 
         if (musicAudioSource)
             musicAudioSource.pitch = 0.5f; // slow down the music
     }
     
     void BeatLevel() {
         // game is over
         gameIsOver = true;
 
         // repurpose the timer to display a message to the player
         mainTimerDisplay.text = "LEVEL COMPLETE";
 
         // activate the gameOverScoreOutline gameObject, if it is set 
         if (gameOverScoreOutline)
             gameOverScoreOutline.SetActive (true);
 
         // activate the nextLevelButtons gameObject, if it is set 
         if (nextLevelButtons)
             nextLevelButtons.SetActive (true);
         
         // reduce the pitch of the background music, if it is set 
         if (musicAudioSource)
             musicAudioSource.pitch = 0.5f; // slow down the music
     }
 
     // public function that can be called to update the score or time
     public void targetHit (int scoreAmount, float timeAmount)
     {
         // increase the score by the scoreAmount and update the text UI
         score += scoreAmount;
         mainScoreDisplay.text = score.ToString ();
         
         // increase the time by the timeAmount
         currentTime += timeAmount;
         
         // don't let it go negative
         if (currentTime < 0)
             currentTime = 0.0f;
 
         // update the text UI
         mainTimerDisplay.text = currentTime.ToString ("0.00");
     }
 
     // public function that can be called to restart the game
     public void RestartGame ()
     {
         // we are just loading a scene (or reloading this scene)
         // which is an easy way to restart the level
         Application.LoadLevel (playAgainLevelToLoad);
     }
 
     // public function that can be called to go to the next level of the game
     public void NextLevel ()
     {
         // we are just loading the specified next level (scene)
         Application.LoadLevel (nextLevelToLoad);
     }
     
 
 }

Projectile Firing script

 public class EnemyProjectile : MonoBehaviour
 {
 
     public Rigidbody projectile;
     public float bulletsPerSecond = 0.5f;
     public float bulletImpulse = 20.0f;
 
     private bool shooting = false;
 
     // Use this for initialization
     void Start()
     {
         InvokeRepeating("Shoot", 0.0f, 1.0f / bulletsPerSecond);
     }
 
     void Shoot()
     {
         if (shooting = true)
         {
             Rigidbody bullet = (Rigidbody)Instantiate(projectile, transform.position + transform.forward, transform.rotation);
             bullet.AddForce(transform.forward * bulletImpulse, ForceMode.Impulse);
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         RaycastHit hit;
         if (Physics.Raycast(transform.position, transform.forward, out hit))
         {
             if (hit.collider.gameObject.tag == "Player")
             {
                 if (EnemyAI.isAlive)
                 {
                     shooting = true;
 
                 }
             }
         }
     }
 }

Thanks for any help in advance, this is my first project ive coded in C# any other suggestions are welcome.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

41 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

Related Questions

How do I change the time on my timer from another script? 1 Answer

How to remove/freeze timer on Destroy() 2 Answers

Timer.deltaTime resetting once and only once instead of whenever I want. 2 Answers

Respawn issues 2 Answers

How do I generate a random number in regular time intervals? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges