Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eganwall · May 27, 2014 at 06:43 PM · performancespawnissuezombieframes

Huge performance hit when spawning prefab with AI

Hey guys, I'm working on a simple top-down shooting game, and I just started having some trouble when I began working on spawning zombies into the world. Basically I have an array of spawn points, and when I press a button a random point is picked and a zombie is spawned. The zombie prefab has a basic AI script attached to it (it just chases the player at this point). The problem I have is that as soon as the first zombie spawns, my game takes a huge performance hit (140 fps down to 11-13 when the zombie spawns). I can't for the life of me figure out what's happening with it, so any help is appreciated. The zombie AI script is as follows:

 #pragma strict
 
 private var target : Transform;
 private var moveSpeed : float = 3.0;
 private var turnSpeed : float = 3.0;
 
 private var myTransform : Transform;
 
 private var health : float = 100;
 
 function Awake()
 {
     myTransform = transform;
 }
 
 function Start()
 {
     target = GameObject.FindWithTag("Player").transform;
 }
 
 function Update()
 {
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), turnSpeed * Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }

I also have a GameController script attached to an empty GameObject in the world, and it controls the spawning of zombies:

 #pragma strict
 
 public var zomPrefab : GameObject;
 
 private var roundNumber : int = 1;
 private var Zombies : ArrayList;
 private var spawnPoints : GameObject[];
 
 
 function Start () 
 {
     spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
 
     Zombies = new ArrayList();
     // var newZom : GameObject = GameObject.Instantiate(zomPrefab, spawnPoints[0].transform.position, Quaternion.identity);
     // Zombies.Add(newZom.gameObject);
 
     // RenderSettings.ambientLight = new Color(.2, .2, .2, 1);
 }
 
 function OnGUI()
 {
     GUI.Label(Rect(0, 0, 50, 100), "Round " + roundNumber);
     GUI.Label(Rect(0, 50, 100, 100), "Zombies: " + Zombies.Count);
     // GUI.Box(Rect(0, 0, 50, 100), roundNumber);
 }
 
 function Update () 
 {
     if(Input.GetKeyDown("tab"))
     {
         NewRound();
     }
 }
 
 function NewRound()
 {
     roundNumber++;
     SpawnZombie();
 }
 
 function SpawnZombie()
 {
     var spawnPoint : int = Random.Range(0, spawnPoints.length);
 
     Debug.Log("Spawning zombie at point " + spawnPoint);
 
     var newZom : GameObject = GameObject.Instantiate(zomPrefab, spawnPoints[spawnPoint].transform.position, Quaternion.identity);
     Zombies.Add(newZom.gameObject);
 }

I know that Instantiate() calls are expensive, but I figured that since I'm not using many that shouldn't be a huge problem. Any thoughts?

Thanks!!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · May 27, 2014 at 06:48 PM 0
Share

Two points:

First, your zombie AI is using Quaternion.Slerp when it should probably be using RotateTowards.

Second, a quick once-over turns up nothing elaborate in these scripts. I suspect the problem is something else.

Try something like this:

  1. Play your game in the editor

  2. Spawn a zombie

  3. Pause the game

  4. Disable one or two components on the zombie

  5. Unpause the game

  6. Has frame rate improved? If yes, you found the problem component. If no, repeat steps 3-6 for another component.

avatar image Narv · May 27, 2014 at 06:59 PM 0
Share

Other than what rutter said, what platform are you targeting and is the FPS drop in the editor or in a build? (mobile, PC, Win8 app, etc).

Do the zombies have a rigidbody on them and have physics going?

Do the draw call numbers go up by chance when you spawn a zombie? (even if you don't have pro you can see this data in the editor, draw calls, poly count, etc)

Try doing a debug statement that tells you the number of zombies in the arraylist in case for some reason it's spawning more than once zombie at a time (and they overlap eachother, maybe?)

avatar image eganwall · May 27, 2014 at 07:08 PM 0
Share

I did what rutter suggested and tried toggling components on the spawned zombies. They only have 2 components (the Zombie script and a capsule collider), and the performance issue seems to stem from having them both active at the same time. Disabling either or both of the components fixes the framerate issue. I'm not sure why this is, though. Any help?

Thanks for the earlier comments by the way, both of you!

EDIT: The zombies do not have a rigidbody on them, and the only physics present is the capsule collider. The FPS drop is in both editor and builds (PC) and draw call numbers don't increase markedly when a zombie is spawned.

avatar image meat5000 ♦ · May 27, 2014 at 08:06 PM 0
Share

Scripts are complete? No OnCollisionStay call?

What happens if collider is made a trigger?

Does it start in a collision? Does the other object have OnCollisionStay routine? $$anonymous$$esh collider - Convex? $$anonymous$$arked as static etc etc?

DropBox it if you want. I or someone will take a look. Don't think its a scripting problem.

avatar image eganwall · May 27, 2014 at 08:32 PM 0
Share

After I get out of class I will try to clean up the project so it's small enough to zip and upload.

The scripts don't have anything weird like OnCollisionStay calls or anything, and if the collider is made a trigger the performance is still bad. The framerate doesn't drop when the object collides, it drops as soon as the first zombie is spawned.

I tried using a RigidBody ins$$anonymous$$d of a collider on the prefab, which resulted in much better performance. However, I'm not quite sure how to make that look the best. $$anonymous$$y model currently is not animated, and I know that with a rigidbody I can check for RaycastHit (my shooting system involves Raycast with a LineRenderer).

I don't have time to mess with it currently (about to go to class), but my question is: will using a RigidBody on the enemy zombies introduce any issues as far as animating the zombie while it runs/attacks? Will it need a collider to keep from falling through the terrain?

Thanks for the help!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 27, 2014 at 08:36 PM

If you have Pro then your first step in diagnosing any performance problem should be to run the Profiler.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Engine issue... 1 Answer

Zombie wave spawn help? 0 Answers

¿can i draw Objects only when in a certain range around the Player? 2 Answers

Problem with spawning script 1 Answer

Unity Code Performance Issue 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges