Hi,
What I am trying to achieve is as follows:
- Player Instantitates particles while on trigger
- Player Moves->Particles Fade
- Player Wants to Immediately instantiate again…
- WaitForSeconds Timer stops
- Reinstantiate particles with the original timer
I apologize for the lengthy code, but I seriously cannot figure this out…
What I am thinking is that step four would prevent the particles from “sputtering” because I do not yet have step four acheived, so the timing is thrown off and the particles sputter.
The code is as follows:
void OnTriggerStay(Collider other)
{
InstantiateParticles();
FadeParticles();
}
void OnTriggerExit(Collider other)
{
if (theRedParticles != null)
{
theRedParticles.particleEmitter.emit = false;
theRedParticles = null;
}
}
void FadeParticles()
{
if (theRedParticles != null)
{
theRedParticles.GetComponent<ParticleEmitter>( ).transform.position = new Vector3(0, 0, 0);
theRedParticles.GetComponent<ParticleEmitter>( ).transform.position = PlayerMoveScript.myPosition;
}
if (Mathf.Abs(PlayerMoveScript.MoveForward) > 0.000001f)
{
Debug.Log("Moved");
PlayerMoved = true;
if (theRedParticles != null)
{
holder = new Color[5];
holder = theRedParticles.GetComponent<ParticleAnimator>( ).colorAnimation;
for (int i = 0; i < 5; i++)
holder[i].a -= 0.01f;
theRedParticles.GetComponent<ParticleAnimator>( ).colorAnimation = holder;
}
}
else // if we stop and holder[0].a is less than 1 (the state that it was before we fade the particles) fade particles
{
if (theRedParticles != null)
{
holder = new Color[5];
holder = theRedParticles.GetComponent<ParticleAnimator>( ).colorAnimation;
if (holder[0].a < 1f)
{
for (int i = 0; i < 5; i++)
holder[i].a -= 0.01f;
theRedParticles.particleEmitter.emit = false;
Debug.Log("Stop Emitting"); // Particles != null
}
theRedParticles.GetComponent<ParticleAnimator>( ).colorAnimation = holder;
}
}
}
void InstantiateParticles()
{
if (Input.GetKeyDown("space") PlayerMoveScript.MoveForward == 0)
{
if (RedParticles != null theRedParticles == null)
{
Debug.Log("Preparing to Bias RED");
theRedParticles = (GameObject) Instantiate(RedParticles, transform.position, Quaternion.identity);
PlayerMoved = false;
StartCoroutine(Wait());
}
}
}
private IEnumerator Wait()
{
while (true)
{
if (theRedParticles != null) yield return new WaitForSeconds(3.5f);
else yield break;
if (theRedParticles != null theRedParticles.particleEmitter.emit)
{
Debug.Log("Stop Emitting");
theRedParticles.particleEmitter.emit = false;
}
if (theRedParticles != null) yield return new WaitForSeconds(3.5f);
else
{
Debug.Log("break after waiting");
yield break;
}
if (theRedParticles == null PlayerMoved == false)
{
Debug.Log("Start Emitting");
theRedParticles = (GameObject) Instantiate(RedParticles, transform.position, Quaternion.identity);
}
}
}
Essentially, we want the Wait() method to break immediately when the player moves while instantiating. Breaking immediately, and pressing space should restart the WaitForSeconds so that the particles do not “sputter” and the timing is not thrown off. But, its not really a “sputter” its the remainder of the particles that haven’t been emitted, that emit if the Wait Method is not exited.
Again, any help, suggestions, hints, would be highly appreciated by many, as we have been stuck for quite soome time.