- Home /
Coroutine Not Working
No idea why this isn't working. The IEnumerator MoveGrenade is to move the grenade above the enemy once it collides with the enemy...which it does. The IEnumerator Capture is supposed to create a line renderer from the grenade to the enemy in increments (basically a line extending from the grenade to the enemy slowly). The problem is after the grenade moves above the enemy, it doesn't create the line. Any suggestions?
using UnityEngine;
using System.Collections;
public class Grenade : MonoBehaviour {
public float throwTime;
public float lineDrawSpeed = 6f;
private LineRenderer line;
private Rigidbody grenadeRigidbody;
private GameObject grenade;
private float counter;
private float distance;
void Awake() {
line = gameObject.GetComponentInChildren<LineRenderer>();
grenadeRigidbody = GetComponent<Rigidbody>();
grenade = gameObject;
}
void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "Enemy"){
StartCoroutine(Capture(col.gameObject));
}
}
private IEnumerator MoveGrenade(GameObject col){
Vector3 moveTo = new Vector3(grenade.transform.position.x, col.transform.position.y+0.5f, grenade.transform.position.z);
while(Vector3.Distance(grenade.transform.position, moveTo) > 0f){
grenadeRigidbody.velocity = Vector3.zero;
grenadeRigidbody.angularVelocity = Vector3.zero;
grenadeRigidbody.Sleep();
grenade.transform.LookAt(col.transform.position);
grenade.transform.position = Vector3.Lerp(grenade.transform.position, moveTo, Time.deltaTime);
yield return null;
}
}
private IEnumerator Capture(GameObject col){
yield return StartCoroutine(MoveGrenade(col.gameObject));
Vector3 pointA = gameObject.transform.position;
Vector3 pointB = col.transform.position;
distance = Vector3.Distance(pointA, pointB);
while(counter < distance){
counter += .1f / lineDrawSpeed;
float x = Mathf.Lerp(0, distance, counter);
Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
line.SetPosition(1, pointAlongLine);
}
yield return null;
}
}
UPDATE - I added "Debug.Log("First coroutine has ended");" after line 35...and it never shows up in the console...so apparently the IEnumerator $$anonymous$$oveGrenade is not actually completing.
Answer by Bunny83 · Nov 15, 2014 at 03:54 AM
You probably want the "yield return null;" in line 53 inside the while loop, don't you? Also you would probably want to use Time.deltaTime when increasing your "counter". Your lineDrawSpeed actually is not a "speed" since if you increase the value it will be slower. I think you want
counter += lineDrawSpeed * Time.deltaTime;
So the value of lineDrawSpeed is actually in units per second.
Last thing is, are you actually setting the position 0 somewhere? You always just move the second point (index "1") of the line renderer.
I moved the "yield return null;" into the while loop... I changed "counter += .1f / lineDrawSpeed;" to "counter += .1f / lineDrawSpeed * Time.deltaTime;"... I added "line.SetPosition(0, pointA);" above line 51...
...but it's still not actually rendering the line. :(
UPDATE - I added "Debug.Log("First coroutine has ended");" after line 35...and it never shows up in the console...so apparently the IEnumerator $$anonymous$$oveGrenade is not actually completing.
Yes, sure ^^ You use the "lerp" function again as smoothing function. Depending on how fast your game runs (so how small deltaTime is) you might never reach your target as you always just move a fraction of the remaining distance towards the target. At some point the amount you want to move might be so small that it's actually rounded down to 0. So the last tiny bit you will never move so the distance will never by exactly 0. It's in general not a good idea to expect exact values when you deal with floats.
So either change the condition to ( > 0.01f) or use a different way to move your grenade. Do you actually want a smoothed decelerated movement?
Yes. At first I was using transform.position.translate, but it just makes the grenade suddenly appear in a different position in 3D space. So I'd like to to actually float/hover up to the new position.
Changing the condition to (> 0.01f) worked.
Answer by ScottYann · Dec 23, 2014 at 05:51 PM
I had this problem also. I had an ienumerator that would not render any lines below a yield WaitForSeconds. I was stumped to and had my brother look at it and he could not find anything wrong with my code.
I executed it with the function reference not the string. I didn't try executing it with a string.
This seems like a problem an engineer should be involved with.
The only solution I had was to run the coroutine on another GameObject. Its ugly but it worked.
Your answer
Follow this Question
Related Questions
Coroutines IEnumerator not working as expected 2 Answers
Use Coroutine when object rotates 2 Answers
How do Coroutines behave with Triggers? 1 Answer
StopCoroutine with IEnumerator not working 2 Answers
coroutine in update method 1 Answer