Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Performance with Unity

Discussion in 'Getting Started' started by Shadowing, Jan 29, 2015.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Hey guys I am new to Unity. This is also my first game engine that I've used. When doing the space shooter tutorial I noticed if I add 100 asteroids it can start feeling not very smooth. I also can kinda notice it when only have 10 asteroids flying by. Seems to be the same on pc build or web player build.

    I'm only using the trailer version of the software at the moment is that why? I would of thought a simple asteroid game would be flawless with lag? Sent the build to a friend he said the same thing.


    Thanks for any replies.
     
  2. Kondor0

    Kondor0

    Joined:
    Feb 20, 2010
    Posts:
    601
    You could download the Pro trial and execute the project with the Profiler running, that should indicate you what's consuming most resources.

    I haven't tried that tutorial but I suggest that you also investigate about pooling. IIRC in the manual there are other tips about improving performance. Remember that there's no engine that can compensate for mistakes or lazy design, the programmer needs to be clever enough to use the resources available and nothing more.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    One thing to remember about tutorials are that they are usually not performance oriented. I have found that most tutorials are not optimized unless its their focus. Trying to figure out why a tutorial is not performing very well can also help you be a better programmer.
     
    angrypenguin likes this.
  4. JamesB

    JamesB

    Unity Technologies

    Joined:
    Feb 21, 2012
    Posts:
    133
    Hey guys,

    This is one of the tutorials I didn't write so I can't help with any details about why it's specifically not performing. However, this is a beginner tutorial and so I can make a few guesses. Our beginner tutorials are based on getting from nothing to making a game in the simplest way we can. As such they won't usually include things like object pooling or any other optimisations. They are to help you understand the features of Unity and the basic structure of games made in Unity. Chances are in the space shooter tutorial gameobjects are being Instantiated and Destroyed rather than pooled. This is likely the source of the performance problem.

    Kondor0 is absolutely correct about using the Profiler. It is an excellent way of determining what is slowing your game down. If you haven't already tried it, I recommend getting a Pro Trail and giving it a go.
     
    angrypenguin likes this.
  5. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Oh ya true. I notice that the tutorial kept always remaking new asteroids instead of storing objects and reusing them. I have a large PHP/javascript background. If I wrote that in javascript I wouldn't be recreating them each time.
     
    angrypenguin likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you write in Unity you typically don't keep creating them either. It just requires some work to implement, so is left out of the beginner tutorials.

    Still, pooling alone shouldn't account for performance issues. There might be something else going on.
     
  8. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    You wouldn't happen to be using .FindComponent<> in updates would you? This would include using the .transform property directly. If you're doing a lot of .transform.somethings, you may want to cache the transform (and any other finds you do) on the Start() of the script like so:
    Code (CSharp):
    1. public class MyClass : MonoBehavior
    2. {
    3.     Transform _transform;
    4.     void Start()
    5.     {
    6.         _transform = gameObject.GetComponent<Transform>();
    7.     }
    8.  
    9.     void Update()
    10.     {
    11.         // _transform.position = something;
    12.     }
    13. }
     
    Kiwasi likes this.
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    So you've got 100+ things moving around and it's getting choppy? My first guess is that the moving objects don't have Rigidbodies. I know it's counter-intuitive, but adding Rigidbodies to moving objects makes them run faster. This is because of internal optimisations in the physics system - if it doesn't have a Rigidbody it's collision data is stored in an optimised collection that's expected not to change, but if you then move the object that data has to be updated, which is expensive. You're potentially doing that 100 times per frame.

    If you don't want physics to be applied set the Rigidbody to be "kinematic" and to not use gravity. Both are tick boxes in the inspector or available by code.
     
    Kiwasi and chrisall76 like this.
  10. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    only for unity 4 though - 5 should have very little difference with or without rigidbody. If there's a difference, file a bug report (for 5)
     
    angrypenguin likes this.
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
  12. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,117
    Isn't this only applicable to 3D physics?