Hey! So I am trying to render a trail, using a Trail Renderer, to draw a road in a 2D game. I can easily do it during the gameplay if I call my function from Update(), but I need the road to be already there by the time I start playing. So I have tried to run the same code in the Start() function (in a for loop), and all variables seem to be updating as expected, but the trail does not render. Here is the code:
void generateRoad (){
//for (int i = 0; i<1000; i++){
cont=cont+Mathf.PI/10;
//Debug.Log (cont);
//Debug.Log (startPos);
if (cont == Mathf.PI/10) {
thisTrail = (GameObject)Instantiate (target,this.transform.position, Quaternion.identity);
}
Ray mRay = Camera.main.ScreenPointToRay (new Vector3(100f*Mathf.Sin (cont/10)+250,cont,-1));
float rayDistance;
if (objPlane.Raycast (mRay, out rayDistance)) {
thisTrail.transform.position = mRay.GetPoint (rayDistance);
//}
}
}
Any ideas as to why it does not work in the Start() function? Also, any suggestion on better ways to draw the road from script? Thank you so much!