Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is the script that handles the player going to sleep. Alot of is dealing with the fade which I'll cover later.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Sleep_Clickable : ClickableParent
{
//DaycountManager is the manager that handles specifically the days system
public DayCountManager DayManager;
public Text DayDisplay;
public Text DifficultyDisplay;
public Image FadeImage;
public float MinimumDistance;
public AudioClip Clip;
private AudioSource Source;
private float currentAlphaOfImage;
void Start(){
Source = GameObject.Find("Player").GetComponent<AudioSource>();
FadeImage.color = new Color(FadeImage.color.r, FadeImage.color.g, FadeImage.color.b, 0);
currentAlphaOfImage = 0;
DayDisplay.enabled = false;
DifficultyDisplay.enabled = false;
}
public override void OnClick(Vector3 ClickPosition){
if(Vector3.Distance(ClickPosition, transform.position) <= MinimumDistance)
Source.PlayOneShot(Clip);
StartCoroutine(Fade());
}
IEnumerator Fade(){
while(FadeImage.color.a < 1){
currentAlphaOfImage += 0.1f;
FadeImage.color = FadeImage.color = new Color(FadeImage.color.r, FadeImage.color.g, FadeImage.color.b, currentAlphaOfImage);
yield return new WaitForSeconds(0.01f);
}
DayManager.NextDay();
DayDisplay.text = "Day: " + DayManager.CurrentDay.ToString();
DayDisplay.enabled = true;
DifficultyDisplay.text = "Difficulty Level: " + PopManager.DifficultyLevel.ToString();
DifficultyDisplay.enabled = true;
yield return new WaitForSeconds(5.0f);
DifficultyDisplay.enabled = false;
DayDisplay.enabled = false;
while(FadeImage.color.a > 0){
currentAlphaOfImage -= 0.1f;
FadeImage.color = FadeImage.color = new Color(FadeImage.color.r, FadeImage.color.g, FadeImage.color.b, currentAlphaOfImage);
yield return new WaitForSeconds(0.01f);
}
currentAlphaOfImage = 0;
FadeImage.color = FadeImage.color = new Color(FadeImage.color.r, FadeImage.color.g, FadeImage.color.b, currentAlphaOfImage);
StopCoroutine(Fade());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment