I am working on an interactive game to aid with anxiety management.
In one of the gameplay options, the player has to press buttons for a certain amount of time. Only after the required time has elapsed (with the player holding the buttons down), will the avatar move. So for example, the instruction will be "Press the buttons for 20 seconds to move the player". This part is working.
As this is an anxiety game, however, I want to incorporate some breathing activity/instruction into the gameplay. I have chosen to use the 4-7-8 second rule, whereby the player breathes in for 4 seconds, holds their breath for 7 seconds and exhales for 8 seconds. I have no idea how to incorporate something like this into my existing game. I understand that a second timer would be needed, but how would that be able to discern between the 4 seconds, 7 seconds and 8 seconds?
Attached is my timer script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour
{
public int timeLeft;
public Text countdownText;
private bool timerStarted;
int start;
public void BeginTimer()
{
StartCoroutine(LoseTime());
}
void Awake()
{
start = timeLeft;
}
public void StopTimer()
{
StopAllCoroutines();
Respawn.Instance.Player2.transform.position = Respawn.Instance.RespawnPoint2.transform.position;
PlayerController.Instance.timerStarted = false;
timeLeft = start;
countdownText.text = "Time Left= " + start;
}
private IEnumerator LoseTime()
{
while (timeLeft > 0)
{
yield return new WaitForSeconds(1);
timeLeft--;
countdownText.text = "Time Left= " + timeLeft;
}
PlayerController.Instance.Character.GetComponent<Rigidbody2D>().AddForce(PlayerController.Instance.height, ForceMode2D.Impulse);
//SphereController.Instance.Player.GetComponent<Renderer>().material;
print("hi");
}
public void ChangeLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
And my playercontroller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public static PlayerController Instance { set; get; }
public float Speed;
public Vector2 height;
public float xMax, xMin, yMin, yMax;
public bool buttonPressed = false;
public Rigidbody2D Character;
public Timer timer;
public Rigidbody2D _rigidbody;
public bool timerStarted = false;
private SerialPort sp = new SerialPort("\\\\.\\COM4", 9600);
Animator anim;
string value;
int button;
private void Awake()
{
FetchReferences();
Instance = this;
//print("hi");
if (AvatarChooser.ava == 1)
{
anim = GameObject.FindWithTag("Player").GetComponent<Animator>();
anim.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("3");
//print("hi");
}
if (AvatarChooser.ava == 2)
{
anim = GameObject.FindWithTag("Player").GetComponent<Animator>();
anim.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("1");
}
if (AvatarChooser.ava == 3)
{
anim = GameObject.FindWithTag("Player").GetComponent<Animator>();
anim.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("2");
}
}
[ContextMenu("FetchReferences")]
private void FetchReferences()
{
if (!Character) Character = GameObject.FindWithTag("Player").GetComponent<Rigidbody2D>();
if (!timer) timer = GameObject.Find("Timer").GetComponent<Timer>();
}
private void Start()
{
if (!sp.IsOpen)
{
sp.Open(); // Open
}
sp.ReadTimeout = 1;
//print(AvatarChooser.ava);
}
private void Update()
{
if (sp.IsOpen)
{
try
{
value = sp.ReadLine(); //Read the information
button = int.Parse(value);
if (button == 9)/*(Input.GetKeyDown(KeyCode.Space))*/ //jump
{
buttonPressed = true;
if (!timerStarted)
{
timerStarted = true;
timer.BeginTimer();
}
}
else
{
timer.StopTimer();
}
}
catch (System.Exception)
{
}
}
}
private void FixedUpdate()
{
Character.position = new Vector3
(
Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
);
}
void OnDisable()
{
sp.Close();
}
}