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

Question Cheap and efficient code for a falling and rotating object.

Discussion in '2D' started by randall_chua, Jan 27, 2022.

  1. randall_chua

    randall_chua

    Joined:
    Oct 5, 2020
    Posts:
    2
    Hello!

    I'm making a simple clicker game to stretch my mobile game dev muscles a lil.

    I'm trying to make my code more performant and efficient.

    Right now, I've managed to use a Sprite Atlas, Dynamic batching and Object Pooling to save on some performance.

    Whenever a button is pressed, it spawns a Monster (made out of a few different sprites) with the following behaviour:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         Transform transform1;
    4.  
    5.         (transform1 = transform).rotation *= Quaternion.Euler(0, 0, Time.deltaTime * rotateSpeed * rotateDir);
    6.         transform1.position += new Vector3(0, -Time.deltaTime * fallSpeed, 0);
    7.        
    8.         if (transform1.position.y < killTresholdY)
    9.             ReturnToPool();
    10.     }
    I was wondering if anyone has any advice to optimise a simple movement code like this?

    Would using Coroutines be better?

    OR also any other optimisation advice you might have.

    Thanks in advance!!
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    1. don't call this.transform (line #5) - you want to cache the transform on the script in Awake().
    tr = this.transform;

    2. declaring Transform transform1 (line #3) like that, then assigning in line #5 is odd and makes things harder to read. You should simply use the local variable (this.tr) for the cached transformed.
    3. Any transform that moves should be a top-level object in the hierarchy (have no parent). See here: https://blog.unity.com/technology/best-practices-from-the-spotlight-team-optimizing-the-hierarchy
    4. This only really matters for things with a hierarchy, but in general if an object has a (0,0,0) parent transform position/rotation then you can get away with using localPosition and localRotation which is more efficient
    5. Coroutines are not better since this is being called every frame. Coroutines are great for things that run infrequently, though they have a slight initialization cost and can be a source of garbage.
    6. If you have a LOT of these, then it may be better to have some MonsterManager script manually call a Tick() method, removing Update(). Updates have some overhead. But if the # isn't like a thousand, then this extra complexity may not be worth it. See here: https://blog.unity.com/technology/1k-update-calls
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    Check out 31:30. Makes most of the same points I made. Plus a neat one about caching the Vector3 I had forgotten, and eliminating Vector math, but I'd really never do that one, not worth readability decrease unless truly desperate.

    Fun talk: