Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class engineStart : MonoBehaviour
{
public bool engineOn = false;
public GameObject engLight;
public GameObject engineActiveText;
public GameObject startingText;
public GameObject idleText;
public GameObject canpressText;
public GameObject pressTimeText;
public GameObject startTimeText;
public AudioSource start;
public AudioSource idle;
public bool canPress = true;
public bool cranking = false;
public bool idling = false;
public bool EngineTimer = false;
public float startupTime = 100;
public float pressTime = 10;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
engineActiveText.GetComponent<Text>().text = "Engine Active: " + engineOn;
startingText.GetComponent<Text>().text = "Crankiing: " + cranking;
idleText.GetComponent<Text>().text = "Idling: " + idling;
canpressText.GetComponent<Text>().text = "CanPress: " + canPress;
pressTimeText.GetComponent<Text>().text = "Press Time: " + pressTime;
startTimeText.GetComponent<Text>().text = "Crank Time: " + startupTime;
//So the EngineSwitch void sets engineOn to true/false.
if (engineOn == false)
{
engLight.SetActive(false);
start.Stop();
idle.Stop();
}
else if (engineOn == true)
{
startupTime -= 1;
engLight.SetActive(true);
}
/* if (canPress == false)
{
pressTime -= 1;
}
if (pressTime < 0)
{
pressTime = 10;
canPress = true;
}*/
//StartupTime is essentially controls the transition from the engine cranking to the engine being active
if (startupTime < 0)
{
startupTime = 100;
cranking = false;
idling = true;
EngineTimer = false;
}
if (cranking == true)
{
start.Play();
cranking = false;
}
if (idling == true)
{
idle.Play();
idling = false;
}
}
/* private void OnTriggerExit(Collider other)
{
if (other.tag == "engStart" && canPress == true)
{
canPress = false;
EngineActive();
}
}*/
public void EngineSwitch(bool engineActive = false)
{
// start engine if not already active
if (engineActive == false)
{
engineActive = true;
engineOn = true;
cranking = true;
EngineTimer = true;
}
//stop engine if active
else if (engineActive == true)
{
engineActive = false;
engineOn = false;
}
}
}