- Home /
Question by
AntAnimeGamer · Nov 05, 2016 at 01:22 PM ·
instantiate2d gamerenderingprofilertimer-script
How to decrease Gfx.WaitForPresent? (2D)
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;
}
}
}
captureunity.png
(106.8 kB)
Comment
Answer by tanoshimi · Nov 05, 2016 at 01:29 PM
WaitForPresent means that your CPU is having to wait for your GPU to be ready before sending the next frame of data. In other words, your scene is taking too long to render - try disabling any image effects on your camera or using simpler shaders.