In my code right as it gets to the IEnumerator LookAtPlayer() unity freezes but all of the audio still plays. It doesn't crash it just freezes and I have to go into task manager to exit out of it. Is there anyway I can fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static LeanTween;
public class PlayerOnEnter : MonoBehaviour
{
public AudioSource audioSource;
public int Toggle = 0;
public GameObject lightOne;
public GameObject lightTwo;
public GameObject lightThree;
public float riseAmount = 1.0f;
public Transform player;
public GameObject objectToTween;
public float tweenSpeed = 1.0f;
public GameObject blockade;
public AudioSource audioSourceTwo;
public GameObject taskTwo;
public GameObject taskThree;
public GameObject targetObject;
public float activateTime = 5f; //time in seconds
public AudioSource audioSourceTask;
public GameObject enemyPrefab;
public BoxCollider spawnArea;
public float spawnInterval = 5f;
public int enemiesPerSpawn = 2;
public AudioSource audioSourceBackground;
public float timer = 0.0f;
//public TextMeshProUGUI timerText;
public Text timerText;
private void Start()
{
targetObject.SetActive(false);
lightThree.SetActive(false);
lightTwo.SetActive(false);
lightOne.SetActive(false);
blockade.SetActive(false);
Debug.Log("lights deactivated!");
InvokeRepeating("Countdown", 1, 1);
audioSourceBackground.Play();
timerText.enabled = false;
}
public void Activate()
{
targetObject.SetActive(true);
Invoke("Deactivate", activateTime);
}
private void Deactivate()
{
targetObject.SetActive(false);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Perform an action when the player enters the trigger
if (Toggle == 0)
{
Debug.Log("Player entered trigger!");
audioSource.Play();
lightOne.SetActive(true);
lightTwo.SetActive(true);
lightThree.SetActive(true);
blockade.SetActive(true);
Toggle += 1;
StartCoroutine(TweenOutOfGround());
}
}
}
IEnumerator TweenOutOfGround()
{
Vector3 startPos = objectToTween.transform.position;
Vector3 endPos = startPos + Vector3.up * riseAmount;
float t = 0.0f;
while (t < 1.0f)
{
t += Time.deltaTime * tweenSpeed;
objectToTween.transform.position = Vector3.Lerp(startPos, endPos, t);
yield return null;
}
StartCoroutine(LookAtPlayer());
}
IEnumerator LookAtPlayer()
{
// timerText.enabled = true;
audioSourceTwo.Play();
taskTwo.SetActive(false);
taskThree.SetActive(true);
Activate();
audioSourceTask.Play();
lightOne.SetActive(false);
lightTwo.SetActive(false);
lightThree.SetActive(false);
InvokeRepeating("SpawnEnemies", spawnInterval, spawnInterval);
audioSourceBackground.Stop();
StartTimer();
while (true)
{
objectToTween.transform.LookAt(player);
yield return null;
}
}
private void SpawnEnemies()
{
for (int i = 0; i < enemiesPerSpawn; i++)
{
Vector3 randomPos = new Vector3(Random.Range(spawnArea.bounds.min.x, spawnArea.bounds.max.x),
Random.Range(spawnArea.bounds.min.y, spawnArea.bounds.max.y),
Random.Range(spawnArea.bounds.min.z, spawnArea.bounds.max.z));
GameObject enemyClone = Instantiate(enemyPrefab, randomPos, Quaternion.identity);
enemyClone.SetActive(true);
}
}
IEnumerator StartTimer()
{
while (true)
{
timer += 1 * Time.deltaTime;
timerText.text = timer.ToString();
}
}
}
↧