Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

50 "enemy" and only 4 FPS!

Discussion in 'Scripting' started by gregorio80, Dec 13, 2014.

  1. gregorio80

    gregorio80

    Joined:
    Apr 25, 2014
    Posts:
    12
    Hello,
    i'm studying Unity and i'm a perfect Newbie.
    I'm trying to make a game of hunting and i've created a map of only 200mt2
    in which i have some rabbits (designed with blender but horribles :p ) that are wandering around.

    After i have spent some hours to code a little IA for the rabbits ..
    ive seen that my FPS falls down under 5 fps if i instantiate only 50 rabbits,,,
    My computer is I5 with 8gigs of ddrIII ram. :(

    I have no experience in optimizing nor in game developing

    So i don't know if the cause is my model designed with blender,
    my script attached to every rabbit or other..

    can you help me?

    I attach some screenshots in order to help you to help me :)

    Code (JavaScript):
    1. #pragma strict
    2. var RabbitState;
    3. var velocity:int=2;
    4. var target : Transform; //the enemy's target
    5. var new_checkpoint:Vector3;
    6. var dist_CP:float;
    7. var dist_Player:float;
    8. function Start () {
    9. animation.animateOnlyIfVisible=true;
    10.  
    11. RabbitState="CercaPuntoLibero";
    12. target = GameObject.FindWithTag("Player").transform; //target the player
    13. }
    14.  
    15. function Update () {
    16. Debug.Log(RabbitState+","+dist_Player+","+target.position);
    17. if (RabbitState=="CercaPuntoLibero") {
    18. if (Input.GetKeyUp(KeyCode.Y)) { LookAway(); }
    19. }
    20. if (RabbitState=="Move") {
    21. velocity=3;
    22. dist_CP=Vector3.Distance(transform.position, new_checkpoint);
    23. dist_Player= Vector3.Distance(transform.position, target.position);
    24.  
    25.     if(dist_CP< 1.0) {    LookAway(); }
    26.     else {
    27. transform.position = Vector3.MoveTowards(transform.position, new_checkpoint, Time.deltaTime * velocity);
    28.     }
    29.     if (dist_Player < 9 ) {animation.Play("corsa"); RabbitState="Flee";}
    30. }
    31.  
    32. if (RabbitState=="Flee") {
    33.  
    34. velocity=8;
    35. dist_Player= Vector3.Distance(transform.position, target.position);
    36. dist_CP=Vector3.Distance(transform.position, new_checkpoint);
    37.     if(dist_CP< 1.0) {    LookAway(); }
    38.     else {
    39. transform.position = Vector3.MoveTowards(transform.position, new_checkpoint, Time.deltaTime * velocity);
    40. }
    41. if (dist_Player > 20) {animation.Play("walk"); RabbitState="Move";}
    42. }
    43.  
    44. }
    45.  
    46.  
    47.  
    48. function LookAway() {
    49. var distwalkp = Random.Range(50.0, 80.0);
    50.  
    51.  
    52. var startingPoint:Vector3 = new Vector3(transform.position.x,1,transform.position.z);
    53. new_checkpoint = new Vector3(startingPoint.x + Random.Range(-distwalkp, distwalkp), 0.0 , startingPoint.z + Random.Range(-distwalkp, distwalkp));
    54. var dist_CP:float=Vector3.Distance(transform.position, new_checkpoint);
    55. var forwardRay:Ray = new Ray(transform.position, new_checkpoint-transform.position);
    56.  
    57. if (Physics.Raycast (forwardRay, dist_CP))
    58. {
    59. //Debug.DrawRay(transform.position, new_checkpoint-transform.position, Color.red,1);
    60. LookAway();
    61. }
    62. else
    63. {
    64. transform.LookAt(new_checkpoint);
    65.     //var c =  GameObject.Find("Cube");
    66.     //Instantiate (c, new_checkpoint, Quaternion.identity);
    67. //Debug.DrawRay(transform.position, new_checkpoint-transform.position, Color.green,2);
    68. RabbitState="Move";
    69. }
    70.  
    71. }
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      282.1 KB
      Views:
      772
    • 2.jpg
      2.jpg
      File size:
      391.1 KB
      Views:
      753
    • 3.jpg
      3.jpg
      File size:
      336.6 KB
      Views:
      767
    Last edited: Dec 13, 2014
  2. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    The first thing I can tell you is this is a programming issue. Your rabbits aren't high poly or anything. (I saw the ugliest *ss kitchen that looked like 100 polys but was several million)

    But I don't see anything crazy in your code. I threw it into unity and tested it on one object, didn't slow anything down. I think it must be some other script. Do you have any other significant scripting?
     
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I've had this issue before with Ai before. Calling a lot of Ai code at once can cause major performance issues. What's your fps like with just a few rabbits ingame? I would suggest disabling some pieces of the rabbits code and narrowing down which piece is causing such a dramatic issue.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Debug.Log makes things very slow.
    using .transform is slow (on 4, but OK on 5)
    using "string" == "string" is slow.
     
  5. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    I didn't study the code very much so this may be completely off base but at first glance it looks like you have an infinite loop (or rather potential infinite loop) going on in your code where line 60 of the LookAway function is calling itself (calling the LookAway function again). It would be much clearer to read if you use the code formatting thingy when posting.
     
  6. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    THIS!

    Turn off all your print and debug.log messages and try again. This has caused me so many problems in the past.
    it may just magically fix everything, no joke

    In my experience, it seems to take about 0.1 seconds to run that function. and you're calling it 50 times per frame.
     
    hippocoder likes this.
  7. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Firstly, Don't use string for states. Use enum or ints as comparing strings isn't the fastest. But that probably isn't your problem.

    Second, remove the debug.logs and check your fps. Debug log can slow down stuff a bit especially if lots of different game objects are calling it every frame. But again, probably not your problem.

    Thirdly, AI loops rarely RARELY need to be called every frame in Update, you should look into coroutines as you can specify how frequently you want them to run. In my game my AI generally runs once every 2 seconds and only every frame in very specific circumtances. This will help in the future if your AI gets more complicated.

    Try to keep your Update methods lean and mean, anything that can be delayed should be a coroutine.

    Fourthly, Consider having a very simple AI until the player gets either within sight or a certain distance. You can use a sphere collider set to trigger to detect when the player gets close. Once the player is close, then switch to a more complicated ai routine.

    I havent been able to test your code, but as a poster said above, there is some recursive calls in the LookAway function. Even if you don't lock your game up with an endless loop, there is the possibility that code is looping a few times each time it is called. Multiply that by 50 rabbits and you might have a problem.
     
  8. gregorio80

    gregorio80

    Joined:
    Apr 25, 2014
    Posts:
    12
    i think i love you ALL! :)
    i have SO MUCH work to do!!

    Thank you!!! :)
     
  9. gregorio80

    gregorio80

    Joined:
    Apr 25, 2014
    Posts:
    12
    Now, with 100 rabbits i have 360/400 fps :)
    Thank you all,

    i'm working on understanding coroutines, now :)
     
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    They're breeding like rabbits.