here’s the scripts for the player and health manager as well as level manager and enemy’s script.
EnemyShooting script.
using UnityEngine;
using System.Collections;
public class EnemyShooting : MonoBehaviour {
public GameObject enemyBullet;
public Transform player;
public float fireDelay = 0.5f;
float cooldownTimer =0;
void Start()
{
if (GameObject.Find ("Player"))
player = GameObject.Find ("Player").transform;
}
void Update ()
{
{
if (player == null)
{
GameObject go = GameObject.Find ("Player");
if (go != null)
{
Shoot();
}
}
if (player == null)
return;
}
}
void Shoot()
{
cooldownTimer -= Time.deltaTime;
if( cooldownTimer <=0 )
{
cooldownTimer = fireDelay;
Instantiate(enemyBullet, transform.position , transform.rotation);
}
}
}
EnemyMovementScript
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour {
public Transform player;
public float moveSpeed = 2;
public float rotateSpeed = 2;
public float maxDistance = 5;
public float restTime;
public float rTime;
public float rotSpeed =180f;
public GameObject Bullet;
void Start ()
{
if (GameObject.Find ("Player"))
player = GameObject.Find ("Player").transform;
}
void Update ()
{
if (player == null)
{
GameObject go = GameObject.Find ("Player");
if (go != null){
player = go.transform;
}
}
if (player == null)
return;
{
if (Vector3.Distance (transform.position, player.position) > maxDistance)
{
transform.position += (player.position - transform.position).normalized * moveSpeed * Time.deltaTime;
}
}
Vector3 dir = player.position - transform.position;
dir.Normalize ();
float zAngle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
Quaternion desiredRot = Quaternion.Euler (0, 0, zAngle);
transform.rotation = Quaternion.RotateTowards (transform.rotation, desiredRot, rotSpeed * Time.deltaTime);
if (player) {
if (Vector3.Distance (transform.position, player.position) < 10)
Shoot ();
}
}
void Shoot ()
{
if (rTime >= 0)
rTime -= Time.deltaTime;
if (rTime <= 0)
if (player)
{
Instantiate (Bullet, transform.position, transform.rotation);
rTime = restTime;
}
}
}
Level manager scripts
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour {
public GameObject currentCheckpoint;
public GameObject deathParticle;
public GameObject respawnParticle;
private PlayerController player;
public int pointPenaltyOnDeath;
public float respawnDelay;
public HealthManager healthMnager;
void Start ()
{
player = FindObjectOfType<PlayerController> ();
healthMnager = FindObjectOfType<HealthManager> ();
}
void Update ()
{
}
public void RespawnPlayer()
{
StartCoroutine ("RespawnPlayerCo");
}
public IEnumerator RespawnPlayerCo()
{
Instantiate (deathParticle, player.transform.position, player.transform.rotation);
player.enabled = false;
player.GetComponent<Renderer> ().enabled = false;
ScoreManager.AddPoints (-pointPenaltyOnDeath);
Debug.Log ("Player Respawn");
yield return new WaitForSeconds (respawnDelay);
player.transform.position = currentCheckpoint.transform.position;
player.enabled = true;
player.GetComponent<Renderer> ().enabled = true;
healthMnager.FullHealth();
healthMnager.isDead = false;
Instantiate (respawnParticle, player.transform.position, player.transform.rotation);
}
}
using UnityEngine;
using System.Collections;
public class KillPlayer : MonoBehaviour {
public LevelManager levelManager;
void Start ()
{
levelManager = FindObjectOfType<LevelManager> ();
}
void Update ()
{
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.name == "Player")
{
levelManager.RespawnPlayer ();
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HealthManager : MonoBehaviour {
public int maxPlayerHealth;
public static int PlayerHealth;
public Slider healthSlider;
Text healthText;
private LevelManager levelManager;
public bool isDead;
private LifeManager lifeSystem;
void Start ()
{
healthText = GetComponent<Text>();
PlayerHealth = maxPlayerHealth;
levelManager = FindObjectOfType<LevelManager>();
lifeSystem = FindObjectOfType<LifeManager>();
isDead = false;
}
void Update ()
{
healthSlider.value = PlayerHealth;
if (PlayerHealth <= 0 && !isDead)
{
PlayerHealth = 0;
levelManager.RespawnPlayer();
lifeSystem.Takelife();
isDead = true;
}
healthText.text = "" + PlayerHealth;
}
void FixedUpdate()
{
if (PlayerHealth < 0)
{
PlayerHealth = 0;
}
if(PlayerHealth > 100)
{
PlayerHealth = 100;
}
}
public static void HurtPlayer(int damageToGivr)
{
PlayerHealth -= damageToGivr;
}
public static void HealPlayer(int HealToTake)
{
PlayerHealth += HealToTake;
}
public void FullHealth()
{
PlayerHealth = maxPlayerHealth;
}
}
Thanks in advance, i want to have the player completely disappeared after he dies, and enemy stops shooting.
Regards.