I have an object being instantiated into the scene every two second and noticed that there are huge rendering spikes from Gfx.WaitForPresent. I am wondering if there is a way to decrease this. I tried switching the instantiate from Update() to FixedUpdate() which seemed to get rid of a spike here and there but didn’t fix the problem. Any ideas of what is causing this?
Here is the profiler:
and the code:
using UnityEngine;
using System.Collections;
public class AddMeteors : MonoBehaviour {
public GameObject MeteorPrefab;
public float InstantiationTimer = 2f;
public int MetNum = 0;
public float speed = 2f;
public float waittime = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
Fire ();
}
void Fire()
{
InstantiationTimer -= Time.deltaTime;
if (InstantiationTimer <= 0f)
{
transform.position = new Vector3(Random.Range(-6f,6f),transform.position.y,transform.position.z);
float xdifmin = transform.position.x + 6;
float xdifmax = 12 - xdifmin;
float min = (Mathf.Atan2 (transform.position.y, xdifmin))*Mathf.Rad2Deg;
float max = (Mathf.Atan2 (transform.position.y, xdifmax))*Mathf.Rad2Deg;
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, Random.Range ((-180 + min),-max));
MetNum += 1;
var Meteor = (GameObject)Instantiate (
MeteorPrefab,
transform.position,
transform.rotation);
Meteor.name = "Meteor"+MetNum;
// Add velocity to the bullet
Meteor.GetComponent<Rigidbody2D> ().velocity = Meteor.transform.right * speed;
InstantiationTimer = waittime;
}
}
}
