Hi,
I’m a beginner in Unity 5 and i’m trying to create a FPS
For the moment, there is only one mission, and there is a problem with the player health. If I die, health got reset to 100, but if I survive until the end and I restart just after, i will have the same amount of health than before. I already tried to reset health just before level start but it doesn’t work
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SurviveMissionScript : MonoBehaviour {
private Scene scene;
public static int PlayerHealth = 100;
void Start () {
PlayerHealth = 100;
}
public void ChangeScene (string sceneName)
{
PlayerHealth = 100;
SceneManager.LoadScene (1);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GlobalHealth : MonoBehaviour {
public static int PlayerHealth = 100;
public int InternalHealth;
public GameObject HealthDisplay;
void Update() {
InternalHealth = PlayerHealth;
HealthDisplay.GetComponent<Text> ().text = "Health: " + PlayerHealth;
if (PlayerHealth == 0) {
Cursor.visible = (true);
SceneManager.LoadScene (2);
PlayerHealth = 100;
}
}
}
There is also an ammo system, with a limited number of ammunition. However, if the Total Ammo is null, i want the reloading to be disabled. But it keeps reloading and decreases the Total Ammo…
Here’s the script for the SMG
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SMG : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public AudioSource SMGShot;
public AudioSource ReloadingSound1;
public int maxAmmo = 25;
public int TotalAmmo = 50;
private int currentAmmo;
public float reloadTime = 1f;
private bool isReloading = false;
public GameObject AmmoDisplay;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
public Animator animator;
void Start ()
{
currentAmmo = maxAmmo;
}
void OnEnable()
{
isReloading = false;
animator.SetBool ("Reloading", false);
}
void Update () {
AmmoDisplay.GetComponent<Text> ().text = "Ammo: " + currentAmmo + "/" + TotalAmmo;
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine (Reload());
return;
}
if (TotalAmmo <= 0 && currentAmmo <= 0)
{
isReloading = false;
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot ();
SMGShot.Play();
}
}
IEnumerator Reload ()
{
isReloading = true;
Debug.Log ("Reloading...");
animator.SetBool ("Reloading", true);
ReloadingSound1.Play ();
yield return new WaitForSeconds (reloadTime - .25f);
animator.SetBool ("Reloading", false);
yield return new WaitForSeconds (.25f);
currentAmmo = maxAmmo;
TotalAmmo -= 25;
isReloading = false;
}
void Shoot ()
{
muzzleFlash.Play ();
currentAmmo--;
RaycastHit hit;
if (Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit))
{
Debug.Log (hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce (-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)) as GameObject;
Destroy(impactGO, 2f);
}
}
}
Third problem, when i shoot too much below, it will touch the FPS controller (the one of the standard assets) instead of the ground
And fourth, even if it is not really a problem, I would like to know, what is for you, the best ay to keep a save of the progress in a game. I want to create a leveling up system, and weapons unlocking system… and I don’t know how and where to start ^^
Thanks for your help