holopy3/Assets/Scripts/DoorOpener.cs

107 lines
2.6 KiB
C#
Raw Permalink Normal View History

2021-01-28 12:07:52 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorOpener : MonoBehaviour
{
public GameObject EndPosition;
public GameObject StartPosition;
private AudioSource source;
private bool oneShot;
private void OnEnable()
{
EnterPyramidTrigger.OnEnterPyramid += StartClosing;
}
private void OnDisable()
{
EnterPyramidTrigger.OnEnterPyramid -= StartClosing;
}
// Start is called before the first frame update
void Start()
{
source = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (!oneShot && Input.GetKeyDown("space"))
{
oneShot = true;
StartCoroutine("OpenDoor");
}
}
private IEnumerator OpenDoor()
{
source.Play();
float timeSinceStarted = 0f;
while (true)
{
timeSinceStarted += Time.deltaTime / 100;
transform.position = Vector3.Lerp(transform.position, EndPosition.transform.position, timeSinceStarted);
// If the object has arrived, stop the coroutine
if (transform.position == EndPosition.transform.position)
{
yield break;
}
// Otherwise, continue next frame
yield return null;
}
}
private void StartClosing()
{
StopAllCoroutines();
StartCoroutine("CloseDoor");
}
private IEnumerator CloseDoor()
{
source.Play();
float timeSinceStarted = 0f;
while (true)
{
timeSinceStarted += Time.deltaTime / 100;
transform.position = Vector3.Lerp(transform.position, StartPosition.transform.position, timeSinceStarted);
// If the object has arrived, stop the coroutine
if (transform.position == StartPosition.transform.position)
{
yield break;
}
// Otherwise, continue next frame
yield return null;
}
}
private IEnumerator MoveFunction()
{
source.Play();
float timeSinceStarted = 0f;
while (true)
{
timeSinceStarted += Time.deltaTime / 100;
transform.position = Vector3.Lerp(transform.position, EndPosition.transform.position, timeSinceStarted);
// If the object has arrived, stop the coroutine
if (transform.position == EndPosition.transform.position)
{
yield break;
}
// Otherwise, continue next frame
yield return null;
}
}
}