As the title says, I am having an issue with prefabs: I am trying to make a bullet follow a curve and then hit a target using this method:
private IEnumerator BezierShooting()
{
GameObject shotInstance = new GameObject();
shotInstance = Instantiate(shotPrefab, transform.position, Quaternion.identity);
Vector3 currentPosition = new Vector3();
if (shotInstance != null)
{
while (t < 1 && shotInstance != null)
{
currentPosition = Mathf.Pow(1 - t, 3) * controlPoint0.position +
3 * Mathf.Pow(1 - t, 2) * t * controlPoint1.position +
3 * (1 - t) * Mathf.Pow(t, 2) * controlPoint2.position +
Mathf.Pow(t, 3) * controlPoint3.position;
Debug.Log(currentPosition);
shotInstance.transform.position = currentPosition;
t += 0.02f;
yield return new WaitForEndOfFrame();
}
}
else
{
currentPosition = transform.position;
t = 0;
yield break;
}
}
Where t
is a float variable that I initialised before as well as the transforms controlPoint0
, controlPoint1
, controlPoint2
and controlPoint3
.
The bullet is succesfully following the path that I created, however the problem is that once the bullet hits an object in position (x, y, z) and gets destroyed, if I call the method again the prefab will spawn in the last position it was on. I thought that final else
statement would do the trick, but appearantly I am missing something. Can somebody help me please?