I am working on a 3d game that involves switching between 3 lanes to avoid obstacles. I am adding a power-up and I have the spawning working, I am currently trying to add a timer that wears off after 5 seconds. My coroutine only works once and skips over the while loop inside it. I have tried starting the coroutine in Start() and Update(), but it only runs the two Debug.Log() commands once, then nothing. I have set the bool for the loop to true in multiple parts of the code but it does not seem to be working. Does anyone know what I am doing wrong? Also sorry, I couldn’t get the code coloured.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerSmoothMovement : MonoBehaviour
{
private Vector3 startPosition = new Vector3 (0, 2, -6);
private Vector3 leftPosition = new Vector3 (-3, 2, -6);
private Vector3 rightPosition = new Vector3 (3, 2, -6);
Animator anim;
private Rigidbody playerRB;
public ParticleSystem particle;
public Collision power;
private float timer;
private bool boolean;
private IEnumerator powers;
private float repeatRate = 0.0f;
void Start()
{
powers = PowerUpTimer();
anim = GetComponent<Animator>();
playerRB = GetComponent<Rigidbody>();
particle.Stop();
timer = 0.0f;
StartCoroutine(powers);
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
if(transform.position == startPosition)
{
anim.SetTrigger("StartToLeft");
}
if(transform.position == rightPosition)
{
anim.SetTrigger("RightToStart");
}
}
if(Input.GetKeyDown(KeyCode.D))
{
if(transform.position == startPosition)
{
anim.SetTrigger("StartToRight");
}
if(transform.position == leftPosition)
{
anim.SetTrigger("LeftToStart");
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Obstacle")
{
playerRB.useGravity = true;
anim.gameObject.GetComponent<Animator>().enabled = false;
playerRB.AddTorque(Vector3.left, ForceMode.Impulse);
playerRB.AddExplosionForce(20.0f, transform.position, 20.0f, 5.0f, ForceMode.Impulse);
particle.Play();
StopAllCoroutines();
}
if(other.gameObject.CompareTag("PowerUp"))
{
boolean = true;
timer = (timer + 5.0f);
StartCoroutine(powers);
}
}
IEnumerator PowerUpTimer()
{
Debug.Log("timer is" + timer);
while(boolean == true)
{
yield return new WaitForSeconds(1);
Debug.Log("hi");
timer = (timer - 1.0f);
particle.Play();
}
Debug.Log("passed while loop");
yield return new WaitForSeconds(1);
}
}