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;
}
}