how do you keep high frame rate with 200 or more Gameobjects?
So I know how to instantiate objects but am running into an issue, that when I get around 40 or more objects in my scene (very love poly no more than 400 tris per) it brings the FPS to 9, originally running 90 +. I have been looking into GPU Instancing but am not sure if this is the route I should be taking. If you have any advice on how I should approach large volumes of models in a scene for a RTS style game I am creating I would love to hear from you.
Planetary Annihilation is an example of scale I would like to achieve but i'll stick with 200 Gameobjects for now.
Thank's for the reply , I did take a look at the profiler and it was due to all of my instantiated objects bogging down the system. Taking Unitycoachs advice I was to bring the frame rates back up.
Answer by UnityCoach · Jan 23, 2017 at 10:58 PM
If you have a high rate of instantiation / destruction of object, you want to use Object Pooling.
Here's also general info on reducing heap allocations.
Hey thank you for the Links, Object pooling is exactly what I was looking for! So I do have another question if you don't $$anonymous$$d me asking. Do you know much about GPU instancing? I found some tutorials online that talk a bit about it. From what I can tell (correct me if I'm wrong) it more so holds a mesh and texture with animations that can become instantiated several hundred times but can't really create objects that hold scripts or colliders. In their example they instantiated 5000 meshes, which was pretty cool and would be awesome to have the scale that big but when I went to implement, I had issues attaching any of the meshes with colliders and scripts. $$anonymous$$aybe I was setting this up wrong but from what I can tell GPU instancing won't allow for implementation like that. Now if GPU instancing can hold colliders and scripts would this be a viable option to use VS Object pooling? I guess what would be the benefit of using one over the other if GPU instancing can handle scripts with colliders attached? If you have more links that would be great, here's the link to the tutorial I am referencing as well if you wanted to see. Anyway thank you for the links and help with the initial question, it's one of the first questions someone has actually replied to!
So I found my main culprit, each object I was instantiating held the script for the object I was creating. Though Object pooling did help get more on the scene I still ran into issues having more than 50 at one given time. What really helped me out was creating a globalized script that controlled any of the objects I created. here's a sample of the code,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ship : $$anonymous$$onoBehaviour {
int shipAttack;
float flightSpeed = .001f;
int shipRange;
float turnSpeed = 3;
public Transform target;
//Rigidbody rbShip;
Quaternion Roatation;
GameObject Beam;
GameObject[] ships;
Vector3 islooking = new Vector3 (0,0,0);
// Use this for initialization
void Start () {
shipAttack = 10;
flightSpeed = 5;
shipRange = 10;
//rbShip = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update() {
ships = GameObject.FindGameObjectsWithTag("ship");
foreach (GameObject i in ships)
{
Rigidbody iShip;
iShip = i.GetComponent<Rigidbody>();
Vector3 targetDir = target.position - i.transform.position;
float step = turnSpeed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(i.transform.forward, targetDir, step, 0.0F);
// Debug.DrawRay(transform.position, newDir, Color.red);
i.transform.rotation = Quaternion.LookRotation(newDir);
//Debug.Log("targetDir: " + targetDir);
if (targetDir != islooking)
{
//Debug.Log("force is running");
//rbShip.AddForce(transform.forward * move);
//float move = flightSpeed * Time.deltaTime;
iShip.AddForce(i.transform.forward * flightSpeed);
//Vector3 new$$anonymous$$ove = Vector3.$$anonymous$$oveTowards(transform.forward, targetDir, move);
//transform.position = Quaternion.LookRotation(new$$anonymous$$ove);
}
}
}
}
after making it all run a for loop I was able to get (at current testing) 500 in the scene at once, without a lick of frame rate reduction. I have been running into the issue, that when my ships get close to one another the frame rate goes way down, but when they spread out a bit it goes right back to 100 fps. So I'm going to make sure it checks to see how far each ship is and spread them out from there. thank you for all your help!
here's a link to what I was able to achieve
Ah, that remembers me my early days in 3D graphics, when you couldn't handle so many objects in a 3D package and had to use Particle Systems that were optimised for mass transform manipulations. :) Well done! It's pretty impressive.
Your answer
Follow this Question
Related Questions
c# Object reference not set to an instance of an object 1 Answer
[SOLVED] Cannot AddForce to instantiated game object. 1 Answer
How to destroy on exiting PlayMode/EditMode? 1 Answer
"Field " " is never assigned to, and will always have it's default value null" 2 Answers
How do i convert game object counts, into int, and put into string? 0 Answers