- Home /
Power System in Unity.
I'm creating a FNAF game, and I need powered doors and I think I need to use a bool that subtracts .1 every second that the bool is true (when door is open) but I have no idea how to start. Can someone please help me?
using UnityEngine; public class Doors : MonoBehaviour {
public GameObject DoorLeft;
public GameObject DoorRight;
public GameObject ChicaVent;
public static bool ChicaVentIsCupcaked = false;
void Start()
{
DoorLeft.SetActive(false);
DoorRight.SetActive(false);
ChicaVent.SetActive(false);
}
void Update()
{
//All we're doing is toggling the doors. If they're active, we deactivate them; if they're inactive, we activate them
if (Input.GetKeyDown(KeyCode.A))
{
DoorLeft.SetActive(!DoorLeft.activeSelf);
}
else if (Input.GetKeyDown(KeyCode.D))
{
DoorRight.SetActive(!DoorRight.activeSelf);
}
else if (Input.GetKeyDown(KeyCode.S))
{
ChicaVent.SetActive(!ChicaVent.activeSelf);
}
//If either of the doors are active, Movement.IsOnStageOfDeath is true, otherwise it's false.
Movement.IsOnStageOfDeath = (DoorLeft.activeSelf || DoorRight.activeSelf);
ChicaVentIsCupcaked = (ChicaVent.activeSelf);
}
}
Comment
I have no idea what you’re trying to do. Please explain.
Best Answer
Answer by blueshark- · Feb 06, 2019 at 10:05 AM
This can be easily done. I wrote a small script that decreases energy by how much you want to / second. You can try and add it into your script and it should work perfectly :)
public bool doorOpen; // Is the door open?
public float startEnergy; // Start Energy, 100%, edit in editor or script
private float energy; // Energy
public float energyDecrease; // Amount you want to decrese per second
private void Start()
{
energy = startEnergy; // Set energy to the startEnergy
}
private void Update()
{
if (doorOpen) // If the door is open decrease the energy
{
Debug.Log("Energy: " + energy); // That it actually works
energy -= Time.deltaTime * energyDecrease;
}
}