Search Unity

When player dies, enemy keeps shooting at the spot the player died in.

Discussion in 'Scripting' started by BrokenhearT, Jul 1, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    here's the scripts for the player and health manager as well as level manager and enemy's script.

    EnemyShooting script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyShooting : MonoBehaviour {
    5.    
    6.     public GameObject enemyBullet;
    7.  
    8.     public Transform player;
    9.    
    10.     public float fireDelay = 0.5f;
    11.     float cooldownTimer =0;
    12.  
    13.      void Start()
    14.     {
    15.         if (GameObject.Find ("Player"))
    16.         player = GameObject.Find ("Player").transform;
    17.     }
    18.  
    19.  
    20.     void Update ()
    21.     {
    22.         {
    23.             if (player == null)
    24.             {
    25.                 GameObject go = GameObject.Find ("Player");
    26.                
    27.                 if (go != null)
    28.                 {
    29.                     Shoot();
    30.                 }
    31.             }
    32.            
    33.             if (player == null)
    34.                 return;
    35.         }
    36.     }
    37.  
    38.     void Shoot()
    39.     {
    40.         cooldownTimer -= Time.deltaTime;
    41.         if( cooldownTimer <=0 )
    42.         {
    43.             cooldownTimer = fireDelay;
    44.            
    45.             Instantiate(enemyBullet, transform.position , transform.rotation);
    46.         }
    47.     }
    48. }

    EnemyMovementScript

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyScript : MonoBehaviour {
    5.  
    6.     public Transform player;
    7.    
    8.    
    9.     public float moveSpeed = 2;
    10.     public float rotateSpeed = 2;
    11.     public float maxDistance = 5;
    12.     public float restTime;
    13.     public float rTime;
    14.     public float rotSpeed =180f;
    15.    
    16.    
    17.     public GameObject Bullet;
    18.  
    19.     void Start ()
    20.     {
    21.         if (GameObject.Find ("Player"))
    22.             player = GameObject.Find ("Player").transform;
    23.     }
    24.    
    25.     void Update ()
    26.     {
    27.         if (player == null)
    28.         {
    29.             GameObject go = GameObject.Find ("Player");
    30.            
    31.             if (go != null){
    32.                 player = go.transform;
    33.             }
    34.         }
    35.        
    36.         if (player == null)
    37.             return;
    38.        
    39.         {
    40.             if (Vector3.Distance (transform.position, player.position) > maxDistance)
    41.             {
    42.                 transform.position += (player.position - transform.position).normalized * moveSpeed * Time.deltaTime;
    43.             }
    44.         }
    45.        
    46.         Vector3 dir = player.position - transform.position;
    47.         dir.Normalize ();
    48.        
    49.         float zAngle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
    50.        
    51.         Quaternion desiredRot = Quaternion.Euler (0, 0, zAngle);
    52.        
    53.         transform.rotation = Quaternion.RotateTowards (transform.rotation, desiredRot, rotSpeed * Time.deltaTime);
    54.        
    55.        
    56.         if (player) {
    57.             if (Vector3.Distance (transform.position, player.position) < 10)
    58.                 Shoot ();
    59.         }
    60.        
    61.     }
    62.    
    63.     void Shoot ()
    64.     {
    65.         if (rTime >= 0)
    66.             rTime -= Time.deltaTime;
    67.        
    68.         if (rTime <= 0)
    69.         if (player)
    70.         {
    71.             Instantiate (Bullet, transform.position, transform.rotation);
    72.             rTime = restTime;
    73.         }
    74.     }
    75. }
    76.  

    Level manager scripts

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelManager : MonoBehaviour {
    5.    
    6.     public GameObject currentCheckpoint;
    7.     public GameObject deathParticle;
    8.     public GameObject respawnParticle;
    9.    
    10.     private PlayerController player;
    11.  
    12.     public int pointPenaltyOnDeath;
    13.  
    14.     public float respawnDelay;
    15.  
    16.     public HealthManager healthMnager;
    17.    
    18.     void Start ()
    19.     {
    20.         player = FindObjectOfType<PlayerController> ();
    21.  
    22.         healthMnager = FindObjectOfType<HealthManager> ();
    23.  
    24.     }
    25.    
    26.     void Update ()
    27.     {
    28.        
    29.     }
    30.    
    31.     public void RespawnPlayer()
    32.     {
    33.         StartCoroutine ("RespawnPlayerCo");
    34.     }
    35.    
    36.     public IEnumerator RespawnPlayerCo()
    37.    
    38.     {
    39.         Instantiate (deathParticle, player.transform.position, player.transform.rotation);
    40.         player.enabled = false;
    41.         player.GetComponent<Renderer> ().enabled = false;
    42.         ScoreManager.AddPoints (-pointPenaltyOnDeath);
    43.         Debug.Log ("Player Respawn");
    44.         yield return new WaitForSeconds (respawnDelay);
    45.         player.transform.position = currentCheckpoint.transform.position;
    46.         player.enabled = true;
    47.         player.GetComponent<Renderer> ().enabled = true;
    48.         healthMnager.FullHealth();
    49.         healthMnager.isDead = false;
    50.         Instantiate (respawnParticle, player.transform.position, player.transform.rotation);
    51.        
    52.     }
    53.    
    54. }
    55.  





    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KillPlayer : MonoBehaviour {
    5.  
    6.     public LevelManager levelManager;
    7.  
    8.     void Start ()
    9.     {
    10.         levelManager = FindObjectOfType<LevelManager> ();
    11.     }
    12.    
    13.     void Update ()
    14.     {
    15.    
    16.     }
    17.  
    18.     void OnTriggerEnter2D (Collider2D other)
    19.     {
    20.         if (other.name == "Player")
    21.         {
    22.             levelManager.RespawnPlayer ();
    23.         }
    24.     }
    25. }
    26.  




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class HealthManager : MonoBehaviour {
    5.  
    6.  
    7.     public int maxPlayerHealth;
    8.     public static int PlayerHealth;
    9.  
    10.     public Slider healthSlider;
    11.  
    12.  
    13.     Text healthText;
    14.  
    15.     private LevelManager levelManager;
    16.  
    17.     public bool isDead;
    18.  
    19.     private LifeManager lifeSystem;
    20.  
    21.     void Start ()
    22.     {
    23.         healthText = GetComponent<Text>();
    24.         PlayerHealth = maxPlayerHealth;
    25.         levelManager = FindObjectOfType<LevelManager>();
    26.         lifeSystem = FindObjectOfType<LifeManager>();
    27.         isDead = false;
    28.     }
    29.    
    30.     void Update ()
    31.     {
    32.         healthSlider.value = PlayerHealth;
    33.  
    34.         if (PlayerHealth <= 0 && !isDead)
    35.         {
    36.             PlayerHealth = 0;
    37.             levelManager.RespawnPlayer();
    38.             lifeSystem.Takelife();
    39.             isDead = true;
    40.         }
    41.  
    42.  
    43.  
    44.         healthText.text = "" + PlayerHealth;
    45.     }
    46.  
    47.     void FixedUpdate()
    48.     {
    49.         if (PlayerHealth < 0)
    50.         {
    51.             PlayerHealth = 0;
    52.         }
    53.  
    54.         if(PlayerHealth > 100)
    55.         {
    56.             PlayerHealth = 100;
    57.         }
    58.     }
    59.  
    60.     public static void HurtPlayer(int damageToGivr)
    61.     {
    62.         PlayerHealth -= damageToGivr;
    63.     }
    64.  
    65.     public static void HealPlayer(int HealToTake)
    66.     {
    67.         PlayerHealth += HealToTake;
    68.     }
    69.  
    70.     public void FullHealth()
    71.     {
    72.         PlayerHealth = maxPlayerHealth;
    73.     }
    74. }
    75.  


    Thanks in advance, i want to have the player completely disappeared after he dies, and enemy stops shooting.


    Regards.
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    You should check in the enemyscript if the player is dead or not. If he is, stop the shooting function. Also you maybe want to use a distance check from the enemy to the player, but I do not know your game environment.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you are checking
    Code (csharp):
    1.  
    2. if(player==null){..}
    3.  
    in a lot of these scripts, player is going to be null if you use Destroy(). Just disabling the control scripts and repositioning it isn't going to affect those if statements.

    A single boolean in the LevelManager script of "isDead" set to true at the start of the coroutine and false at the end could be used and then referenced in those if statements so "player exists and player isn't dead... do stuff"