I resolved the problem.
When my players health goes to 0 it subtracts a life. This works and puts the player back at the start but then immediately ends the game. Is there a better solution?
https://dl.dropbox.com/u/56545886/StarHawk/StarHawk.html WEBPLAYER
using UnityEngine;
using System.Collections;
public class scrGameController : MonoBehaviour {
//public Camera cameraFollowWithZoom; // Camera 1 rear view
public Transform transformPlayer; // Character transform
private CharacterController characterControllerPlayer; // Character controller
public Transform deathZoneTransform; // Death zone plane, if player falls below it, he dies
public float speed;
private TextMesh healthText;
private TextMesh percent;
public float playerHealth;
public int minPlayerHealth = 0;
public int maxPlayerHealth = 100;
public bool percent25 = false;
private TextMesh textScore;
public int score;
public int points = 0;
private TextMesh lives;
public int playerLife;
public GameObject player;
public Transform spawnLocationPlayer;
// Use this for initialization
void Awake()
{
if(Application.loadedLevelName != "Stage11")
{
points = PlayerPrefs.GetInt("currentScore");
playerLife = PlayerPrefs.GetInt("currentLives");
}
}
void Start ()
{
playerHealth = maxPlayerHealth;
healthText = GameObject.Find("3DTextHealth").GetComponent<TextMesh>();
textScore = GameObject.Find("3DTextScore").GetComponent<TextMesh>();
lives = GameObject.Find("3DTextLives").GetComponent<TextMesh>();
healthText.text = playerHealth.ToString();
textScore.text = points.ToString();
playerLife = 2;
lives.text = playerLife.ToString();
}
void Update()
{
//InvokeRepeating("UpdatePoints" , 10,5, 0.1f);
if(playerHealth <= minPlayerHealth)
{
//playerHealth = minPlayerHealth;
transform.position = new Vector3(transform.position.x,-1.3f, transform.position.z); // if player leaves the screen will pop up on oppisite side
SubtractLife();
//StartCoroutine(Death());
}
if(playerHealth <= 25)
{
healthText.renderer.material.color = Color.red;
}
healthText.text = playerHealth.ToString();
textScore.text = points.ToString();
lives.text = playerLife.ToString();
Debug.Log("Player health: " + playerHealth);
float moveAmout = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime; //moves player left and right
transform.Translate(Vector3.right * moveAmout); // moves player left and right
float moveAmout2 = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime;
transform.Translate(Vector3.up * moveAmout2);
if(transform.position.x <= -6.1f) //// if player leaves the screen will pop up on oppisite side
{
transform.position = new Vector3(5.8f, transform.position.y, transform.position.z); // if player leaves the screen will pop up on oppisite side
}
if(transform.position.x >= 6.1f) //// if player leaves the screen will pop up on oppisite side
{
transform.position = new Vector3(-5.8f, transform.position.y, transform.position.z); // if player leaves the screen will pop up on oppisite side
}
if(transform.position.y <= -4.573847f) //// if player leaves the screen will pop up on oppisite side
{
transform.position = new Vector3( transform.position.x, 4.573845f, transform.position.z); // if player leaves the screen will pop up on oppisite side
}
if(transform.position.y >= 4.573847f) //// if player leaves the screen will pop up on oppisite side
{
transform.position = new Vector3(transform.position.x,-4.573845f, transform.position.z); // if player leaves the screen will pop up on oppisite side
}
}
public void UpdatePoints()
{
points += 1; //add x amount points
textScore.text = "" + " " + points; //update 3D text with the points
}
public void addScoreKill ()
{
points = points + 100;
}
IEnumerator Death()
{
//vp_FPSPlayerScript.enabled = false;
float timeToWait = 2.4f;
yield return new WaitForSeconds(timeToWait);
SubtractLife();
Application.LoadLevel(Application.loadedLevel);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag ==("Enemy"))
{
playerHealth = playerHealth - 25.0f;
}
}
public void leftButton ()
{
transform.Translate(Vector3.right * -1 * Time.deltaTime); // moves player left and right
}
public void rightButton ()
{
transform.Translate(Vector3.right * 1 * Time.deltaTime); // moves player left and right
}
public void upButton ()
{
transform.Translate(Vector3.up * 1 * Time.deltaTime); // moves player left and right
}
public void downButton ()
{
transform.Translate(Vector3.up * -1 * Time.deltaTime); // moves player left and right
}
public void CheckDeathZone()
{
if(transformPlayer.position.y < deathZoneTransform.position.y)
{
transformPlayer.position = new Vector3(0, 1, 0);
}
}
void GameOver()
{
if(points > PlayerPrefs.GetInt("highScore")) //if current points > stored survivalScore (highscore)
{
PlayerPrefs.SetInt("highScore", points); //save the new score
PlayerPrefs.Save();
//audio.PlayOneShot(thirdSound); //play the sound
}
ToMenu(); //load main menu
}
void NextLevel()
{
PlayerPrefs.SetInt("currentScore" , points); //save the current score
PlayerPrefs.Save();
if(Application.loadedLevelName != "Level4")//Level8 //if we haven't reached last level
{
Application.LoadLevel(Application.loadedLevel + 1); //load next level
}
else
{
if(points > PlayerPrefs.GetInt("highScore")) //if current points > stored survivalScore (highscore)
{
PlayerPrefs.SetInt("highScore", points); //save the new score only if it's greater than the current highscore
PlayerPrefs.Save();
}
ToMenu(); //else load main menu
}
}
void ToMenu()
{
Application.LoadLevel("MenuScene"); //load main menu
}
void SubtractLife()
{
playerLife -= 1; //subtract x amount from playerLife
// Instantiate(player,spawnLocationPlayer.transform.position,spawnLocationPlayer.transform.rotation);
//transform.position = new Vector3(transform.position.x,-1.3f, transform.position.z); // if player leaves the screen will pop up on oppisite side
maxPlayerHealth = 100;
if (playerLife > 0 ) //if player life is greater than 0
{
Debug.Log("MainGame: Player lives left :" + playerLife); //spit out the debug log
PlayerPrefs.SetInt("currentLives", playerLife); //save playerLife temporarily (for next level)
// Instantiate(player,transformPlayer.transform.position,transform.rotation);
}
if(playerLife <= 0) //if we have 0 lives
{
Debug.Log("MainGame: Player has no more lives left, returning to main menu"); //spit out the debug log
GameOver(); //enter GameOver function
}
}
}