Hello, I started using Unity this weekend for the first time. Actually first time for any game/app stuff ever. And first post! :mrgreen:
I am making a 2D game that has about 2000 balls which fall down. All are instantiated prefabs with circle colliders, and a script for some scoring. At lower numbers, like 500, this runs fine. At higher numbers like 2000 the game is very laggy on both my PC and Android.
My Questions Are:
Is this just too much for today’s average hardware/software?
Should I change my methodology/scripts?
If the answer is to optimize my methodology/scripts, then here is a more detailed description of my methodology and the associated scripts.
BallGenerator.js instantiates the balls (and is run from an empty GameObject):
// JavaScript
#pragma strict
var hundoballs : GameObject;
function Start () {
for (var y = 0; y < 32; y++) {
for (var x = 0; x < 16; x++) {
Instantiate(BallGenerator, Vector3 (x-15, y+20, 0)*.6, Quaternion.identity);
}
}
}
The prefab is a small PNG (32px X 32px) and has rigidbody2d, circlecollider2d and BallController.cs for keeping track of the scoring and moving the balls back to the top of the screen.
BallController.cs
using UnityEngine;
using System.Collections;
public class BallController : MonoBehaviour {
private Transform spawnPoint;
public bool respawnTop = false;
void OnBecameInvisible()
{
if (respawnTop == true)
{
// float xMax = Camera.main.orthographicSize - 0.5f;
// transform.position = new Vector3( Random.Range (-xMax, xMax), spawnPoint.position.y, transform.position.z );
transform.position = new Vector3(spawnPoint.position.x, spawnPoint.position.y, transform.position.z);
respawnTop = false;
}
}
// Use this for initialization
void Start () {
spawnPoint = GameObject.Find("SpawnPoint").transform;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D playerScore)
{
respawnTop = true;
}
}