- Home /
Bad Android performance
Just started Unity and am trying to port one of my older games to it. I have only 9 sprites on screen and I'm getting really bad performance on my phone.
Statistics for reference. Any ideas? I'm testing it out on a Nexus 5
Building to a Philips C12? :)
Stats look fine. $$anonymous$$ust be a bad script.
The only thing in my script is:
'void Update () {
isGrounded = Physics2D.OverlapCircle (grounder.transform.position, radiuss, ground);
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && isGrounded==true || Input.Get$$anonymous$$ouseButtonDown(0) && isGrounded==true)
{
rigidbody2D.AddForce(jumpVector, Force$$anonymous$$ode2D.Force);
}
}'
Also I just updated to the latest Unity and noticed my game went from 10mb to 18mb on Android
Add a null check to your OverlapCircle result. It's also probably expensive to do in Update.
Don't forget if statement parameters evaluate from left to right. Checking isGrounded twice is a waste of cycles, especially if its null.
Note that this function will allocate memory for the returned Collider2D object. You can use OverlapCircleNonAlloc to avoid this overhead if you need to make the check frequently.
Answer by Baste · Dec 16, 2014 at 01:42 PM
Your fps is almost 3k. That doesn't sound like bad performance to me...
Anyways, you could improve the performance of the if-check slightly by doing this instead (it's more readable anyway):
if(isGrounded && (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0)))
I'd actually expect the compiler to do that already, but the compiler we're sitting on is less than good, so that might not be the case.
Also, since you're not doing anything with the overlapcircle's returned results, simply use nonalloc overlap and send in an empty array. Allocating an array 2800 times every second will eat some memory.
Collider2D[] results = new Collider2D[0];
void Update() {
bool isGrounded = Physics2D.OverlapCircleNonAlloc(transform.position, 2f, results) > 0;
}
Finally, you could extract the whole ground check thing to a coroutine, and do it less often:
bool isGrounded;
void Start() {
StartCoroutine(GroundCheck());
}
IEnumerator GroundCheck() {
Collider2D[] results = new Collider2D[0];
while(true) {
isGrounded = Physics2D.OverlapCircleNonAlloc(transform.position, 2f, results) > 0;
//Check 10 times each second instead of 2800 times
yield return new WaitForSeconds(.1f);
}
}
void Update() {
if(isGrounded && (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0)))
rigidbody2D.AddForce(jumpVector, ForceMode2D.Force);
}
You should probably also put in a cooldown on the jump, as there will be quite a few frames after you've jumped that the player still haven't jumped.
This shouldn't have left moderation @Baste. It is an incomplete question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174127im_/https://answers.unity.com/themes/thub/images/avi.jpg)