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
Your answer