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

Strage slowdown on IOS

Discussion in 'Scripting' started by Fluzing, Jun 24, 2014.

  1. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    The following code works fine in Unity. It hardly costs any resources. However, when I build for Ipad, it causes everything to slow down to a few frames per second. When I remove it, everything is fine.

    I have no idea why.

    This code is part of Update()

    Code (csharp):
    1.  
    2. transform.localScale = Vector3.MoveTowards(transform.localScale, Vector3.zero, Time.deltaTime * 0.3f);
    3.  
    The object the script is attached to has 6 children and there is no other code altering the localScale.

    Anyone have suggestions?
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    My guess: Are you scaling a collider with no rigidbody?
     
  3. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    I agree. It's looks like the only plausible explanation.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Also, are you using Unity Free or Unity Pro? Pro has a profiler that can help narrow down what's causing slow down.
     
  5. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    It has a ridigbody and collider, but those are attached to a child of the gameobject for rotation and mesh collider purposes. Should I only scale those then, and not all the objects?

    Also, I am using Unity free, unfortunately.

    Edit: sorry it does not have a rigidbody. Is it mandatory for something like this and why does it cause slowdowns?
     
  6. bzor

    bzor

    Joined:
    May 28, 2013
    Posts:
    35
    Unity treats all objects with a collider but no rigidbody as static. All static colliders are baked together for the simulation, so when you scale one of them the whole static geometry needs to be updated, which is super expensive.
     
  7. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Is there a way to get around this without adding a rigidbody?
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If you are changing a collider, you should have a rigidbody attached. Or else it's going to cause a ton of recalculations in PhysX.

    Is there a reason you don't want to add a rigidbody?
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Pretty much this. It's what they're for.

    If you don't want it to be a "physics object" then you should set isKinematic to true and useGravity to false. This essentially means the physics system will treat it as a dynamic collider.
     
  10. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    I have a spaceship that gets hit by projectiles. I had rigidbodies on the projectiles and on impact they were destroyed. I have now removed those rigidbodies and placed them on the spaceships.

    The collisions work fine that way, however the collisions influence the rotation and speed of the ship and I don't want that. If I turn on kinematic, collisions don't work anymore.

    Edit: if I put rigidbodies back on the projectiles again, it seems to work if I don't make those kinematic, but shouldn't it work without a rigidbody on the projectile as well? I have a lot of projectiles flying around. Won't that impact performance as well?

    Edit2: I forgot about OnTriggerEnter. That seems to work perfectly now. Thanks for the help everyone!
     
    Last edited: Jun 25, 2014
  11. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    After testing the new build with collider AND rigidbody attached to the ship, the problem is still there. Could it be that scaling a mesh collider is just really expensive?
     
  12. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  13. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    I fixed it by temporary disabling the collider. Not the most elegant of sollution. Not sure how many triangles, but I am guessing alot.