campfire/Assets/Scripts/TutorialController.cs

48 lines
1.3 KiB
C#
Raw Normal View History

2020-04-20 05:03:27 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2020-04-21 00:30:20 +02:00
[RequireComponent(typeof(AudioSource))]
2020-04-20 05:03:27 +02:00
public class TutorialController : MonoBehaviour {
2020-04-21 00:13:46 +02:00
public KeyCode ToggleKey;
public bool Toggled = true;
public float AnimationLerpFactor = 10f;
2020-04-21 00:30:20 +02:00
public AudioClip[] OpenClips;
public AudioClip[] CloseClips;
2020-04-21 00:13:46 +02:00
private RectTransform Rect;
2020-04-21 00:30:20 +02:00
private AudioSource Audio;
2020-04-20 05:03:27 +02:00
private void Awake() {
2020-04-21 00:13:46 +02:00
Rect = GetComponent<RectTransform>();
2020-04-21 00:30:20 +02:00
Audio = GetComponent<AudioSource>();
2020-04-20 05:03:27 +02:00
}
private void Start() {
2020-04-21 00:13:46 +02:00
Rect.anchoredPosition = new Vector2(-100, -100);
2020-04-20 05:03:27 +02:00
}
private void Update() {
2020-04-21 00:13:46 +02:00
if (Input.GetKeyDown(ToggleKey)) {
Toggled = !Toggled;
2020-04-21 00:30:20 +02:00
if (Toggled) {
Audio.PlayOneShot(GetRandomClip(OpenClips));
} else {
Audio.PlayOneShot(GetRandomClip(CloseClips));
}
2020-04-20 05:03:27 +02:00
}
2020-04-21 00:13:46 +02:00
Vector2 Pos = Rect.anchoredPosition;
Pos.y = Mathf.Lerp(Pos.y, Toggled ? -100 : (Rect.rect.height + 10), AnimationLerpFactor * Time.deltaTime);
Rect.anchoredPosition = Pos;
2020-04-20 05:03:27 +02:00
}
2020-04-21 00:30:20 +02:00
private AudioClip GetRandomClip(AudioClip[] clips) {
if (clips.Length > 0) {
return clips[Random.Range(0, clips.Length)];
} else {
return null;
}
}
2020-04-20 05:03:27 +02:00
}