Search Unity

Bug Invincibility and Kill Player after Time Limit

Discussion in 'Scripting' started by AnUnifiedDev, Sep 27, 2022.

  1. AnUnifiedDev

    AnUnifiedDev

    Joined:
    Jul 18, 2019
    Posts:
    2
    Hello, all.

    I do realize I'm "cheating" in the title by including and trying to get help on two separate issues, but I am desperate for answers at this point, so my apologies in advance.

    What I'm trying to do are two things:

    1. After the player collects a power-up, the player can destroy a box that would normally kill the player. After a certain amount of time, the power-up will go away. (I already have the code for the actual collecting the powerup and its time limit.)

    2. After the timer runs to 0, the player is immediately destroyed. (I already have the code for the actual time done.)

    Two problems, however.

    - The code I currently have for Invincibility also destroys the player. Not to mention, you can destroy the Kill Box even if you didn't collect the powerup. (Somehow, this is the best working one so far, as previous code didn't do anything except kill the player.)

    - The timer runs out yet the player is still alive and can move.

    I've been at this all day and have been pulling my hair out, so I want to gauge where I'm going wrong. Below is relevant code. Thank you in advance, all.

    1. Destroy Hazards Class

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Collider))]
    6.  
    7. public class DestroyHazards : MonoBehaviour
    8. {
    9.  
    10.     private Collider _collider;
    11.  
    12.     private void Awake()
    13.     {
    14.         _collider = GetComponent<Collider>();
    15.     }
    16.     private void OnTriggerEnter(Collider other)
    17.     {
    18.         //Searches for player ship.
    19.         PlayerShip playerShip = other.gameObject.GetComponent<PlayerShip>();
    20.         if (playerShip != null) //If the container is not empty, executes code.
    21.         {
    22.             Destroy(gameObject);
    23.         }
    24.     }
    25. }
    2. Invincibility Class (portion)

    Code (CSharp):
    1. private void ActivatePowerup(PlayerShip playerShip)
    2.     {
    3.         playerShip.Invincible(true);
    4.    
    5.         if (_collectParticlePrefab != null)
    6.         {
    7.             Instantiate(_collectParticlePrefab, transform.position, transform.rotation);
    8.         }
    9.     }
    10.  
    11.  
    12.     private void DeactivatePowerup(PlayerShip playerShip)
    13.     {
    14.         playerShip.Invincible(false);
    15.     }
    16. }
    3. Player Ship class (Kill Ship and Invincibility only provided)

    Code (CSharp):
    1. public void KillShip()
    2.     {
    3.         Debug.Log("L + Ratio + Skill Issue");
    4.         this.gameObject.SetActive(false);
    5.     }
    6.  
    7. ***
    8. public void Invincible(bool isInvincible = false)
    9.     {
    10.         if (isInvincible)
    11.         {
    12.             DestroyHazards destroy = new DestroyHazards();
    13.         }
    14.     }
    4. Countdown Class

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Countdown : MonoBehaviour
    7. {
    8.     //Countdown variables.
    9.     [SerializeField] public float currentTime;
    10.     [SerializeField] public bool countDown;
    11.  
    12.     //Time Limit Variables
    13.     [SerializeField] public bool _hasLimit;
    14.     [SerializeField] public float _timerLimit;
    15.  
    16.     [SerializeField] public TextMeshProUGUI timerText;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.  
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         //Can increase or decrease time if countdown is true.
    28.         currentTime = countDown ? currentTime -= Time.deltaTime : currentTime += Time.deltaTime;
    29.  
    30.         if (_hasLimit && (countDown && currentTime <= _timerLimit) || (!countDown && currentTime >= _timerLimit))
    31.         {
    32.             currentTime = _timerLimit;
    33.             SetTimerText();
    34.             OutOfTime();
    35.             timerText.color = Color.red;
    36.             enabled = false;
    37.  
    38.         }
    39.  
    40.         SetTimerText();
    41.     }
    42.  
    43.     private void SetTimerText ()
    44.     {
    45.         timerText.text = currentTime.ToString("0.0");
    46.     }
    47.  
    48.     private void OutOfTime ()
    49.     {
    50.         PlayerShip playerShip = gameObject.GetComponent<PlayerShip>();
    51.         if (playerShip != null) //If the container is not empty, executes code.
    52.         {
    53.             if (currentTime == 0)
    54.             {
    55.                 PlayerShip.Destroy(gameObject);
    56.  
    57.             }
    58.         }
    59.     }
    60. }
    EDIT: After some more tinkering around and some rest and also just using my goddamned brain, I got my Timer function to work. Now it kills the player whenever the time runs out and stops if the player dies before time out. I am still, however, having trouble with the Invincibility idea.



    Again, thank you in advance.
     
    Last edited: Sep 28, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Please edit your post to use code-tags when posting code.

    Thanks.